Jump to content

VBA syntax error message on Access connection


ajs

Recommended Posts

Complete VBA rookie here:

 

I was going through an autodesk tutorial on connecting to Access. Part of the tutorial is the following code:

 

 

 

Public wksObj As Workspace

Public dbsObj As Database

Public tblObj As TableDef

Public fldObj As Field

Public rstObj As Recordset

 

 

Private Sub CommandButton1_Click()

On Error Resume Next

Set wksObj = DBEngine.Workspaces(0)

Set dbsObj = wksObj!CreateDatabase("mydbase.mdb", dbLangGeneral)

Set tblObj = dbsObj!CreateTableDef("mytable")

With tblObj

.Fields.Append .CreateField("text", dbText)

.Fields.Append .CreateField("integer", dbInteger)

.Fields.Append .CreateField("long", dbLong)

.Fields.Append .CreateField("double", dbDouble)

.Fields.Append .CreateField("boolean", dbBoolean)

.Fields.Append .CreateField("memo", dbMemo)

.Fields.Append .CreateField("currency", dbCurrency)

.Fields.Append .CreateField("date", dbDate)

End With

dbsObj.TableDefs.Append tblObj

dbsObj.TableDefs.Refresh

End Sub

 

 

 

Private Sub CommandButton2_Click()

On Error Resume Next

Set dbsObj = DBEngine.Workspaces(0).OpenDatabase("mydbase.mdb")

Set rstObj = dbsObj.OpenRecordset("mytable", dbOpenTable)

rstObj.AddNew

rstObj!Text = "a text value"

rstObj!integer = 1

rstObj!double = 3.1415926

rstObj!Boolean = True

rstObj!currency = "$4,240.54"

rstObj!Date = "2/10/00"

rstObj.Update

rstObj.Close Set dbsObj = Nothing

End Sub

 

 

 

Private Sub CommandButton3_Click()

Unload Me

End Sub

 

 

I'm getting a syntax error on the last code line for command button 2 (Highlighted in red bold)

 

The error I'm getting is: Compile error / Syntax error.

 

Can anyone enlighten me on what the syntax error is? I'm assuming that it has something to do with closing dbsObj and setting it to nil.

 

Thanks...

 

 

AJS

Link to comment
Share on other sites

It may be as simple as:

 

rstObj.Close

Set dbsObj = Nothing

 

in other words, put each statements on a separate line.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...