Hi,
I'm trying to create an update query that will enter a code into a field called "AFCCode" next to a code in another field called "code" so wherever the code in "code" appears as 11 the code in "AFCCode should be AFC34. This will hopefully be done from two textboxes.
I've tried a few variations of the code below to send the data from the boxes but I have had no success so far. It is creating the query but it is a Select query and there is no table in it.
Hope I've explained this ok and any help would be appreciated.
This is the code i'm trying to use:
Private Sub cmd2_Click()
' Pointer to error handler
On Error GoTo cmd2_Click_err
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strwhere As String
Dim strcode As String
Dim strafccode As String
Dim strSQL As String
' Identify the database and assign it to the variable
Set db = currentdb
' Check for the existence of the query, create it if not found,
' and assign it to the variable
If Not QueryExists("qryaddcode") Then
Set qdf = db.CreateQueryDef("qryaddcode")
Else
Set qdf = db.QueryDefs("qryaddcode")
End If
If Len(strwhere) Then strwhere = "WHERE" & Mid(strwhere, 4)
' Build the SQL string
strSQL = "UPDATE tbltruststaff SET tbltruststaff.AFCCode = forms!frmprintreportdialog1!txt2"
WHERE tbltruststaff.Code = [Forms]![frmprintreportdialog1]![Txt1]
strwhere
' Pass the SQL string to the query
qdf.SQL = strSQL
' Turn off screen updating
DoCmd.Echo False
' Check the state of the query and close it if it is open
If Application.SysCmd(acSysCmdGetObjectState, acQuery, "qryaddcode") = acObjStateOpen Then
DoCmd.Close acQuery, "qryaddcode"
End If
' Open the query
DoCmd.OpenQuery "qryaddcode"
cmd2_Click_exit:
' Turn on screen updating
' Clear the object variables
Set qdf = Nothing
Set db = Nothing
Exit Sub
cmd2_Click_err:
' Handle errors
MsgBox "An unexpected error has occurred." & _
vbCrLf & "Please note of the following details:" & _
vbCrLf & "Error Number: " & Err.Number & _
vbCrLf & "Description: " & Err.Description _
, vbCritical, "Error"
Resume cmd2_Click_exit
End Sub
I'm trying to create an update query that will enter a code into a field called "AFCCode" next to a code in another field called "code" so wherever the code in "code" appears as 11 the code in "AFCCode should be AFC34. This will hopefully be done from two textboxes.
I've tried a few variations of the code below to send the data from the boxes but I have had no success so far. It is creating the query but it is a Select query and there is no table in it.
Hope I've explained this ok and any help would be appreciated.
This is the code i'm trying to use:
Private Sub cmd2_Click()
' Pointer to error handler
On Error GoTo cmd2_Click_err
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strwhere As String
Dim strcode As String
Dim strafccode As String
Dim strSQL As String
' Identify the database and assign it to the variable
Set db = currentdb
' Check for the existence of the query, create it if not found,
' and assign it to the variable
If Not QueryExists("qryaddcode") Then
Set qdf = db.CreateQueryDef("qryaddcode")
Else
Set qdf = db.QueryDefs("qryaddcode")
End If
If Len(strwhere) Then strwhere = "WHERE" & Mid(strwhere, 4)
' Build the SQL string
strSQL = "UPDATE tbltruststaff SET tbltruststaff.AFCCode = forms!frmprintreportdialog1!txt2"
WHERE tbltruststaff.Code = [Forms]![frmprintreportdialog1]![Txt1]
strwhere
' Pass the SQL string to the query
qdf.SQL = strSQL
' Turn off screen updating
DoCmd.Echo False
' Check the state of the query and close it if it is open
If Application.SysCmd(acSysCmdGetObjectState, acQuery, "qryaddcode") = acObjStateOpen Then
DoCmd.Close acQuery, "qryaddcode"
End If
' Open the query
DoCmd.OpenQuery "qryaddcode"
cmd2_Click_exit:
' Turn on screen updating
' Clear the object variables
Set qdf = Nothing
Set db = Nothing
Exit Sub
cmd2_Click_err:
' Handle errors
MsgBox "An unexpected error has occurred." & _
vbCrLf & "Please note of the following details:" & _
vbCrLf & "Error Number: " & Err.Number & _
vbCrLf & "Description: " & Err.Description _
, vbCritical, "Error"
Resume cmd2_Click_exit
End Sub