Results 1 to 5 of 5
  1. #1
    DADAZHU is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2011
    Posts
    10

    Simple Update SQL

    Hi,



    I used the following SQL without success, I thought it should be easy. Can anyone help please?

    strSQL = "UPDATE tblUnit SET Unit_Name = '" & strUName & "' WHERE CC = '" & strCC & "'"

    Debug.print showed: UPDATE tblUnit SET Unit_Name = '' WHERE CC = '', both strCC and strUName have been checked before the SQL is executed.

    I also tested strSQL = "UPDATE tblUnit SET Unit_Name = 'AAA' WHERE CC = 'A01'", and it worked.

    Many thanks.

  2. #2
    pdebaets is offline Competent Performer
    Windows Vista Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Los Angeles
    Posts
    235
    First, make sure you have "Option Explicit" at the top of your module, just below "Option Compare Database"

    Compile your modules.

    Next, put a

    debug.print strUName & ", " & strcc

    ... just above where you set strSQL. post the results back here.

  3. #3
    DADAZHU is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2011
    Posts
    10
    Hi, Peter,

    Thanks for your help. I actually have the following codes in an excel spreadsheet from which I read unit info and update the tblUnit table in Access if there are any differences. And I do have "Option Explicit" at the top of my module

    I did what you suggested, the results are as following,

    Code:
     
    AAA,243
    UPDATE tblUnit SET Unit_Name ='' WHERE CC =''
    The routines are as below,

    Code:
    Private Sub 
    btnUpdateUnitInfoInDB_Click()
     
    Dim strSQL As String
    Dim strCC As String, strUName As String
    Dim i As Long
     
    strSQL = "UPDATE tblUnit SET Unit_Name ='" & strUName & "' WHERE CC ='" & strCC & "'"
    Call OpenCN
     
    On Error GoTo ErrHander
    For i = 2 To 116 
    strUName = Me.Range("B" & i).Value
    strCC = Me.Range("A" & i).Value
    Debug.Print strUName & "," & strCC
     
    cn.Execute strSQL
    Debug.Print strSQL
     
    Next i
    Call CloseCN
     
    Exit Sub
    ErrHander:
    MsgBox "Error!"
    Call CloseCN
    End Sub

  4. #4
    pdebaets is offline Competent Performer
    Windows Vista Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Los Angeles
    Posts
    235
    You are setting the value of the variables AFTER you've created the SQL statement. Try this:

    Code:
    Private Sub
    btnUpdateUnitInfoInDB_Click()
      
    Dim strSQL As String
    Dim strCC As String, strUName As String
    Dim i As Long
      
    
    Call OpenCN
      
    On Error GoTo ErrHander
    For i = 2 To 116 
    strUName = Me.Range("B" & i).Value
    strCC = Me.Range("A" & i).Value
    Debug.Print strUName & "," & strCC
    strSQL = "UPDATE tblUnit SET Unit_Name ='" & strUName & "' WHERE CC ='" & strCC & "'"
      
    cn.Execute strSQL
    Debug.Print strSQL
      
    Next i
    Call CloseCN
      
    Exit Sub
    ErrHander:
    MsgBox "Error!"
    Call CloseCN
    End Sub

  5. #5
    DADAZHU is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2011
    Posts
    10
    You are a champ. Thanks Peter.

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

Similar Threads

  1. Replies: 4
    Last Post: 11-10-2011, 03:41 PM
  2. Replies: 1
    Last Post: 01-22-2011, 12:23 PM
  3. Replies: 0
    Last Post: 10-21-2010, 08:24 AM
  4. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 PM
  5. Simple Export Not So Simple
    By jgelpi16 in forum Programming
    Replies: 11
    Last Post: 09-01-2010, 07:23 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