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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help with forms and buttons 1

Status
Not open for further replies.

Javamahn

Technical User
Mar 14, 2001
143
US
I am trying to add records to a table using a button. I know this is simple enuff but I am not familiar with Access SQL to do this. Both the form and the table are named transactions. The form also had a subform on it displaying the table after filtering the data through a SQL statement. There are 3 unbound textboxes an a 4th parameter of (now) which need to be INSERT into the table when CheckIn Item is clicked. Please help

TIA
 
Hallo,

To find the SQL to do this, write an append query using th builder, then switch to SQL view and copy it into your code.
Use the following function to run an SQL Query:
Function ysnRunSQL(ByVal pstrSQL As String) As Boolean
On Error GoTo Err_ysnRunSQL
DoCmd.SetWarnings False
If ysnQueryExists(pstrSQL) Then
DoCmd.OpenQuery pstrSQL, acViewNormal
Else
DoCmd.RunSQL pstrSQL, True
End If
ysnRunSQL = True
Exit_ysnRunSQL:
DoCmd.SetWarnings True
Exit Function
Err_ysnRunSQL:
ysnRunSQL = False
Resume Exit_ysnRunSQL
End Function
All you have to remember is that string variable comparisons need to be in quotes, numeric ones do not.
So:

if not ysnRunSQL("INSERT INTO tblTable ( ID, strTest )
SELECT " & Me!txtNumeric & " AS Expr1, '" & Me!txtText & "' AS Expr2;") then msgbox "Could not update tbl1Table",vbExclamation

will add a new record to tblTable, setting the number field called ID to the value in the current form control called txtNumeric and the text field strTest to the string in the control txtText.

Hope this helps,

- Frink

P.S. If you have any trouble the reply with details of the table to be updated and the form controls and I'll try again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top