WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Access

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Use the OpenTable Mmethod

Keywords: 	  opentable method

Question:

What is the command to open a table in Access using OLE. Is it OPENFORM, OPENTABLE? The example that was given to me (below) does not work.
MyDB = ObjectOpen("Access.Application")
dbname = "c:\temp\access.mdb"
;MyDB.visible = 1
MyDB.visible = @true
MyDB.usercontrol = 1

MyDB.OpenCurrentDatabase(dbname)

MyDB.DoCmd.Openform "System"
myform = MyDB.Forms(0)
MyDB.DoCmd.openform myform
myform = "Test"
MyDB.DoCmd.openform myform,,,,,

;message("",myform)

message("MS Access","Close Access")

;timedelay(3)
;ObjectClose(MyDB)

Answer:

First of all, you are using DAO, which is a subset of OLE

With DAO you often need to open a DAO Workspace Object - then set your database and table objects from the DAO workspace.

>MyDB.DoCmd.Openform "System"
Second, the above syntax is improper for Winbatch; you need to first open the DoCmd Object
oAPP = MyDB.Application
Cmd = oAPP.DoCmd
Then hopefully you can call
Cmd.OpenForm( "System" )
but you are probably better off with: Cmd.RunMacro( "my macro" ) which opens the form and does other processing... just a thought...

Here is one example that uses the 'OpenTable' method to open an access table.

db = "c:\Temp\Test.Mdb"
tablename = "NameTable"
column1 = "FirstName"
column2 = "LastName"
val1 ="Fred"
val2 = "Flintstone"

if !FileExist(db)
         message("Error","Database file doesn't exist")
         exit
endif

AccessDB = ObjectOpen("Access.Application")
ADB = AccessDB.Application
ADB.OpenCurrentDatabase(db)
ADB.Visible = @TRUE

ADBCmd = ADB.docmd

ErrorMode(@off)
ADBCmd.OpenTable(tablename)
Errormode(@cancel)
if lastError() == 1261
   ;need to create the table
        SQLstmt = StrCat("CREATE TABLE ",tablename," (",column1," CHAR, ",column2," CHAR)")
        ADBCmd.RunSQL(SQLstmt )
endif

SQLstmt = StrCat('INSERT INTO ',tablename,' (',column1,',',column2,') VALUES ("',val1,'","',val2,'")')
ADBCmd.RunSQL(SQLstmt )
acTable =  0
acSaveYes = 1
ADBCmd.Close(acTable, tablename, acSaveYes)
ADBCmd.Quit

ObjectClose(adbcmd)
ObjectClose(adb)
ObjectClose(accessdb)

Article ID:   W15248
File Created: 2002:09:05:13:50:50
Last Updated: 2002:09:05:13:50:50