Insert Record into Table Using VB Code
Posted on 2008-06-22
I have a button on my form that automatically generates a report based on a query. Except for the address field (which should be filled in by the system) the data on the report is all previous data entry done by the users. I want to fix it so when the user clicks the button the system first looks in tblNames for the individual on whom the report is being run and checks to see if the address field is blank. If so the system should assign the next available address in qryAddresses to the field tblNames.Address for that individual and then print the report showing the address that was just saved in the table. In other words the report should never open with the address blank. The only time that field will be blank is the first time the user runs the report. The system will then assign the address if its blank. If the address field is not blank when the user clicks the button the code will continue and simply open the report with the address previously assigned.
Private Sub Command296_Click()
On Error Goto Err_Command296_Click
Dim strSQL As String
Dim CurAdd As Integer
Dim SysAdd As Integer
CurAdd=DLookup("[Current Address]","qry4EMrpt", "[EMNumber]= ' " & Forms!sfrmEmg![EMNumber] & " ' And [EML]= ' " & Forms!sfrmEmg![EML] & " ' ")
SysAdd = DLookup ("[SysAddNumber]", "qrySysAddresses")
If IsNull(CurAdd) then
strSQL= "INSERT INTO tblNames(CurrentAddress) SELECT (SysAdd) FROM qrySysAddresses WHERE [Name Number]=[Name Number];"
Docmd.openReport "rptEmn", acNormal
Now I get the error message " You canceled the previous operation." Nothing else happens. Any ideas?