How can I use a variable value as a table field name in Access?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeanydoggy
    New Member
    • Jul 2013
    • 2

    How can I use a variable value as a table field name in Access?

    I need to use a variable value as a table field name. But when I ran the code, it gave me error and didn't use variable value but used the variable as the field name. I defined the following Sub and in my main Sub I call this Sub to input variable D, then the updating query will update the field where field name is matched with the variable D value.

    Code:
    Public Sub HC(D As String)
        
        Dim db As DAO.Database
        Dim strSQL As String
        Dim str1 As String
        
        strSQL = "UPDATE [HC Table] INNER JOIN [Previous Day Balances] " _
               & " ON ([HC Table].Loan = [Previous Day Balances].loannum) " _
               & " AND ([HC Table].Seq = [Previous Day Balances].seq) " _
               & " AND ([HC Table].[Claim Type] = [Previous Day Balances].[Claim Type]) " _
               & " SET [HC Table].D = [Previous Day Balances].[net_claim_balance] " _
               & " WHERE ((([Previous Day Balances].net_claim_balance)<>0));"
        
    
        DoCmd.RunSQL strSQL
           
    End Sub
    Please help me Thx.
    Last edited by Rabbit; Jul 18 '13, 10:45 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    You are in the wrong forum. Your thread has been moved to the Access forum.

    You need to append the value of the variable to the string you're building. Not include the variable as a string.
    Code:
    " ... " & variableName & " ... "

    Comment

    • jeanydoggy
      New Member
      • Jul 2013
      • 2

      #3
      Thx for the reply and help me moving to the correct forum.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Just a tip - I think you'll make things much easier on yourself in the future if you use meaningful variable names. If you're maintaining a program you wrote months or years ago, a name like D is likely to tell you very little about what it's for.

        Comment

        Working...