Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable to update table 2

Status
Not open for further replies.

MicroZealous

IS-IT--Management
Dec 5, 2003
56
US
I just learned how to use DLOOKUP to set a public variable.
Now I want to write that variables value back into the table. I have looked in help files, but don't know the words I'm searching for! Just a hint or a simple example. TIA.
 
Do you want to add a new record to the table (APPEND query) or change an existing value (UPDATE query)?


Randy
 
Here's my design: tblSettings holds public variables to survive program closing. When access starts, I use DLOOKUP to populate the public variables, always from tblSettings.ID = 1. This is working. VBA code may update any of variables as needed. OK. Now I want to save the variables as needed, by Update tblSettings, so this would be an update. I will also want to append the variables to tblLog as a diagnostic tool. (For context, I'm experienced with tables, queries, forms and reports, but new to VBA. Can I code SQL statements in VBA?) I'm trying.
 

To create and run the queries in code, something like...
Code:
Private Sub cmdButtonName_Click()
   Dim strSQL as String
   strSQL = "UPDATE myTable "
   strSQL = strSQL & "SET myField = '" & [COLOR=blue]VariableName[/color] & "' "
   strSQL = strSQL & "WHERE [i]criteria here[/i]"
   DoCmd.SetWarnings False
   DoCmd.RunSQL strSQL
   DoCmd.SetWarnings True
End Sub
Remove the single quotes if the field is numeric.

Or, build a query and do something like this...
Code:
Private Sub cmdButtonName_Click()
   DoCmd.SetWarnings False
   DoCmd.OpenQuery [COLOR=bule]myQueryName[/color]
   DoCmd.SetWarnings True
End Sub



Randy
 
You can do something like this:
Code:
CurrentDB.Execute "UPDATE tblSettings SET Field1 = " & varField1 & ", Field2 = " & varField2 & " WHERE ID = 1"


 
Thanks for your help. I really did try the Help file, etc. What I needed was the syntax:
"SET myField = '" & VariableName & "' "

Mike R
 
If the field is a string, you need the quotes. If it is a number, you don't use quotes.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top