Results 1 to 4 of 4
  1. #1
    devesa is offline Novice
    Windows Vista Access 2007
    Join Date
    May 2010
    Posts
    8

    Error in form code

    Hello. In the EVENT CODE of a Access Form I get an error on the line in bold. What I do is to compare two tables (Table1 and Table2) checking if for the same Id, fields name and surname are the same. Otherwise it shows a message. Any ideas? THANKS

    Table1: Id, name, surname


    Table2: Id, name, surname

    Code:
     
    Option Compare Database
    Private Sub Command0_Click()
    On Error GoTo Err_Command0_Click
    Dim strSQL1, strSQL2, strSQL3 As String
    Dim strMessage As String
    Dim rs1, rs2, rs3 As Object
    strSQL1 = "Select * From [Table1]"
    strSQL2 = "Select * From [Table2]"
    Set rs1 = CurrentDb.OpenRecordset(strSQL1)
    Set rs2 = CurrentDb.OpenRecordset(strSQL2)
    If rs2.EOF And rs2.BOF Then
    strMessage = MsgBox("ERROR: No rows to add")
    End If
    Do While Not rs2.EOF
    strSQL3 = "Select * From [Table1] WHERE Id = rs2!Id"
    Set rs3 = CurrentDb.OpenRecordset(strSQL3)
    If rs3!name <> rs2!name Or rs3!surname <> rs2!surname Then
    strMessage = MsgBox("ERROR: Check ID")
    Else
    strMessage = MsgBox("Correct!!")
    End If
    rs2.MoveNext
    Loop
    rs2.Close
    Set rs2 = Nothing
    Exit_Command0_Click:
        Exit Sub
    Err_Command0_Click:
       MsgBox Err.Description
       Resume Exit_Command0_Click
     
    End Sub
    And also... how could I include an UPDATE SQL operation just after the strMessage = MsgBox("Correct!!") line? Any example?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    There's no line in bold, but I suspect the error is because of this line, which should be (presuming ID is numeric):

    strSQL3 = "Select * From [Table1] WHERE Id = " & rs2!Id

    You could run SQL with

    CurrentDb.Execute "UPDATE..."

    with the same kind of concatenation as above. A FAQ on creating SQL in VBA:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    devesa is offline Novice
    Windows Vista Access 2007
    Join Date
    May 2010
    Posts
    8
    Thank you, that was exactly the problem and now is solved. It was really kind and helpful.

    Just a further question. My UPDATE operation is very very long, and if I do
    CurrentDb.Execute "UPDATE............" I get "line too long" error. What can I do?

    Thank you once again!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can use line continuation characters (space-underscore) to break up the line:

    Code:
    Dim strSQL As String
    strSQL = "INSERT INTO TableName (FieldList) " _
           & "Values (ValueList)"
      
    CurrentDb.Execute strSQL, dbFailOnError
    Though if you're updating the data in one of the recordsets it might make more sense to do the update there.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error in Code
    By Lockrin in forum Programming
    Replies: 3
    Last Post: 02-25-2010, 03:27 PM
  2. Simple Nav Form Code Not Working
    By alsoto in forum Forms
    Replies: 10
    Last Post: 04-10-2009, 09:30 AM
  3. Replies: 23
    Last Post: 03-26-2009, 06:50 PM
  4. Replies: 1
    Last Post: 08-10-2008, 01:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums