Results 1 to 5 of 5
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    UPDATE query not recognizing variable as field name

    I get a 3061 runtime error with the following code. I'm assuming the UPDATE query is not recognizing "ChkBox" as a variable string containing the field name. If that is in fact the case, what's the syntax required so that the "ChkBox" string is understood as a field name?



    Code:
    Private Sub SetTable(ChkBox As String)
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  From the name of the check box control, we can determine which column and
    '  hense which table and field needs to be initialized.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    On Error GoTo Err_Handler
    
    
    Select Case left(ChkBox, 4)
        Case "ck11": CurrentDb.Execute "UPDATE tblCol1 SET chkbox = True"          'User number 1, period 1
        Case "ck12": CurrentDb.Execute "UPDATE tblCol2 SET chkbox = True"          'User number 1, period 2
        Case "ck21": CurrentDb.Execute "UPDATE tblCol3 SET chkbox = True"          'User number 2, period 1
        Case "ck22": CurrentDb.Execute "UPDATE tblCol4 SET chkbox = True"          'User number 2, period 2
    End Select
    
    
    Exit_Handler:
       Exit Sub
    Err_Handler:
       MsgBox "Error " & Err.Number & " attempting to initialize column table : " & Err.Description
       Resume Exit_Handler
       
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    So you would concatenate the variable into the sql string?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You haven't concatenated the variable into your SQL string.


    Code:
     Case "ck11": CurrentDb.Execute "UPDATE tblCol1 SET " & chkbox & " = True"
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    GOOD GRIEF! Of course! Age is taking its toll. (Have no fear, one day you'll get yours )
    Thanks,
    Bill

    EDIT: YIKES, I thought I was done with this! None of the column (tblColx) tables got updated, though no errors were issued?
    Code:
    Private Sub SetTable(ChkBox As String)
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  From the name of the check box control, we can determine which column and
    '  hense which table and field needs to be initialized.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    On Error GoTo Err_Handler
    
    
    Select Case left(ChkBox, 4)
        Case "ck11": CurrentDb.Execute "UPDATE tblCol1 SET " & ChkBox & " = True"          'User number 1, period 1
        Case "ck12": CurrentDb.Execute "UPDATE tblCol2 SET " & ChkBox & " = True"          'User number 1, period 2
        Case "ck21": CurrentDb.Execute "UPDATE tblCol3 SET " & ChkBox & " = True"          'User number 2, period 1
        Case "ck22": CurrentDb.Execute "UPDATE tblCol4 SET " & ChkBox & " = True"          'User number 2, period 2
    End Select
    
    
    If Me.Dirty Then Me.Dirty = False
    
    
    Exit_Handler:
       Exit Sub
    Err_Handler:
       MsgBox "Error " & Err.Number & " attempting to initialize column table : " & Err.Description
       Resume Exit_Handler
       
    End Sub
    I ran the code in Debug and all looked as expected, but still don't see the table fields being updated.
    Click image for larger version. 

Name:	000.jpg 
Views:	15 
Size:	69.4 KB 
ID:	48089

    EDIT: Just so you'll know the table properties:
    Click image for larger version. 

Name:	001.jpg 
Views:	14 
Size:	49.8 KB 
ID:	48090

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Comment out the error handler in case it's hiding something?
    What happens if you manually run the update statement.

    I'd also manoeuvre your code around so you know it's running;

    Code:
    Private Sub SetTable(ChkBox As String)
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  From the name of the check box control, we can determine which column and
    '  hense which table and field needs to be initialized.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    'On Error GoTo Err_Handler
    
    
    Dim strSQL as String
    
    
    Select Case left(ChkBox, 4)
        Case "ck11": strSQL =  "UPDATE tblCol1 SET " & ChkBox & " = True"          'User number 1, period 1
        Case "ck12": strSQL =  "UPDATE tblCol2 SET " & ChkBox & " = True"          'User number 1, period 2
        Case "ck21": strSQL =  "UPDATE tblCol3 SET " & ChkBox & " = True"          'User number 2, period 1
        Case "ck22": strSQL =  "UPDATE tblCol4 SET " & ChkBox & " = True"          'User number 2, period 2
    End Select
    debug.print strSQL
    Currentdb.Execute strSQL
    
    
    If Me.Dirty Then Me.Dirty = False
    
    
    
    
    Exit_Handler:
       Exit Sub
    Err_Handler:
       MsgBox "Error " & Err.Number & " attempting to initialize column table : " & Err.Description
       Resume Exit_Handler
       
    End Sub
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. VBA for entering a variable in an UPDATE Query
    By mainerain in forum Programming
    Replies: 6
    Last Post: 08-23-2021, 06:07 AM
  2. MACRO variable not recognizing data in a table
    By gblystone in forum Queries
    Replies: 12
    Last Post: 08-10-2020, 04:34 PM
  3. Replies: 3
    Last Post: 11-30-2018, 05:03 PM
  4. Replies: 3
    Last Post: 10-30-2014, 08:45 AM
  5. Passing a value from a variable to an update query
    By MUKUDU99 in forum Programming
    Replies: 0
    Last Post: 08-24-2008, 11:14 PM

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