Can't update access table that contains space from Excel VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Esmi
    New Member
    • Nov 2008
    • 1

    Can't update access table that contains space from Excel VBA

    I am developing an application in Ms Excel 2003 that has a feature to update records in Ms Access. The problem is the table name I am trying to update contains a space and whenever I run the below code I got the following Error Message.

    [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected ‘DELETE’, ‘INSERT’, ‘PROCEDURE’, ‘SELECT, OR ‘UPDATE’

    However, I can’t rename the table because it has many relationships and connections with other tables.

    Private Sub CommandButton1_ Click()

    On Error Resume Next

    Dim adoconn As ADODB.Connectio n
    Dim adors As ADODB.Recordset


    Set adoconn = New ADODB.Connectio n
    adoconn.Open "Provider=MSDAS QL.1;Persist Security Info=False;Data Source=Requirem ents"


    Set adors = New ADODB.Recordset

    adors.Open "[EmployeeList1]", adoconn, adOpenDynamic, adLockOptimisti c

    MsgBox Err.Description
    MsgBox Err.Source

    Do While Not adors.EOF

    If adors.Fields("` Ey Number`").Value = Range("A1").Val ue Then
    adors.Fields("` Ey Number`").Value = "123"

    Exit Do

    End If
    adors.MoveNext
    Loop
    adors.Update
    adors.Close
    End Sub



    Could you help me to sort out this problem?
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Originally posted by Esmi
    I am developing an application in Ms Excel 2003 that has a feature to update records in Ms Access. The problem is the table name I am trying to update contains a space and whenever I run the below code I got the following Error Message.

    [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected ‘DELETE’, ‘INSERT’, ‘PROCEDURE’, ‘SELECT, OR ‘UPDATE’

    However, I can’t rename the table because it has many relationships and connections with other tables.

    Private Sub CommandButton1_ Click()

    On Error Resume Next

    Dim adoconn As ADODB.Connectio n
    Dim adors As ADODB.Recordset


    Set adoconn = New ADODB.Connectio n
    adoconn.Open "Provider=MSDAS QL.1;Persist Security Info=False;Data Source=Requirem ents"


    Set adors = New ADODB.Recordset

    adors.Open "[EmployeeList1]", adoconn, adOpenDynamic, adLockOptimisti c

    MsgBox Err.Description
    MsgBox Err.Source

    Do While Not adors.EOF

    If adors.Fields("` Ey Number`").Value = Range("A1").Val ue Then
    adors.Fields("` Ey Number`").Value = "123"

    Exit Do

    End If
    adors.MoveNext
    Loop
    adors.Update
    adors.Close
    End Sub



    Could you help me to sort out this problem?
    Hi

    Just a thought, but if "EmployeeLi st1" is a query (?) then you need this

    adors.Open "SELECT * FROM EmployeeList1", adoconn, adOpenDynamic, adLockOptimisti c


    Also, does your field name actualy have apostrophies, if not, then you do not need then, just

    adors.Fields("E y Number")

    should do it.


    ??


    MTB

    Comment

    Working...