add records button?

elmop2005

New member
Local time
Today, 15:45
Joined
Feb 9, 2012
Messages
8
I am trying to create a button on a sub form to add the records on it to another sub form on the same for, if that makes any sense!

below is my current code and it works sort of, it picks the the records and values but doesn't put it onto the other form.
any ideas?


Private Sub AddOrderbtn_Click()
On Error GoTo Err_AddOrderbtn_Click
DoCmd.SetWarnings False '..cancel system warnings
MsgBox "INSERT INTO T_Order_line(ProductID, Description, Price, Stock Level)" _
& " VALUES " & [ProductID] & ", " & [Description] & ", £" & [Price] & ", Level: " & [Stock Level] & ""
Forms![F_Orders].[Product Catalogue].Requery
Exit_AddOrderbtn_Click:
Exit Sub
Err_AddOrderbtn_Click:
MsgBox Err.Description
Resume Exit_AddOrderbtn_Click
End Sub



Private Sub ProductCombo_AfterUpdate()
If ProductCombo.Value <> "" Then
ProductID.SetFocus '..set the focus to the field returned by the combo box
DoCmd.FindRecord ProductCombo.Value, acAnywhere, False, acSearchAll, False, acCurrent, True
End If

End Sub
 
You need DoCmd.RunSQL or CurrentDb.Execute to execute that query.

By the way, I would advise that you Debug.Print your SQL statement and look in the Immediate Window to ensure it's well formed. I suspect you have syntax errors as it stands.
 
Also, your use of

DoCmd.SetWarnings False '..cancel system warnings

is very bad (you never turn them back on, which can be very dangerous) because it also means that if you make a change in the design view of a form, query, report, anything it will not prompt you to save if you have run that and not turned them back on. So you need to save explicitly or it will discard your changes automatically.

The DoCmd.SetWarnings True should be in the Exit part of your procedure like this (so if an error occurs it still gets turned back on):

Code:
Private Sub Test()
On Error GoTo Test_Err
 
DoCmd.SetWarnings False
 
' do whatever you had going on here
 
Test_Exit:
   DoCmd.SetWarnings True
   Exit Sub
 
Test_Err:
Msgbox Err.Description, vbExclamation, Err.Number
Resume Test_Exit
 
End Sub
 

Users who are viewing this thread

Back
Top Bottom