I have a form (FindForm) that I use to retrieve a record in a MySQL database and then open a second unbound form (MainForm) displaying numerous fields from the record. In the following code I am able to open the form successfully and things seem to work ok but the user may choose to return to the FindForm and search for another record in which case the FindForm does not close as it should and I cannot get focus on the Main form. It also does not display the correct record info in the Main Form. The Form_Activate event on the main Form contains the instructions to bind the data from the records to the controls of the form as well as set up many other display features. I'm not sure if the Form_Activate event is the best way to go about doing this? Looking for any suggestions on the best way to go about doing this.
Private Sub cmdFindJob_Click()
On Error GoTo Err_Handler
strJobno = Me.Jobno
strDept = Me.dept
Dim rst As ADODB.Recordset
Dim myconn As ADODB.Connection
Set myconn = SetMyConn
strSql = "SELECT jobid FROM usijobs as j " & _
"WHERE j.jobno = '" & strJobno & "' AND dept = '" & strDept & "';"
Set rst = New ADODB.Recordset
rst.Open strSql, myconn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Not rst.EOF Or Not rst.BOF Then
rst.MoveFirst
lngJobSetupOpenargs = rst!jobid
strFrmName = "USIJobSetup"
If Application.CurrentProject.AllForms(strFrmName).IsLoaded Then
Debug.Print "Form is loaded"
Forms.Item(strFrmName).SetFocus
' DoCmd.Close acForm, "USIFindJobSetup", acSaveNo
Else
DoCmd.OpenForm strFrmName, acNormal, , , acFormEdit, acWindowNormal, rst!jobid
End If
Dim jsform As Form
Set jsform = Application.Forms(strFrmName)
jsform.Controls("Jobno") = strJobno
jsform.Controls("Dept") = strDept
DoCmd.Close acForm, "USIFindJob", acSaveNo
Else
MsgBox "No Job record exisits for the criteria you have provided.", vbOKOnly, "NO JOB RECORD"
End If
rst.Close
Set rst = Nothing
myconn.Close
Set myconn = Nothing
Exit_cmdFindJob_Click:
Exit Sub
Err_Handler:
Dim erADO As ADODB.Error
For Each erADO In myconn.Errors
Select Case erADO.NativeError
Case 1045
Debug.Print erADO.Number & " " & erADO.description & " " & erADO.SQLState & " " & erADO.NativeError
Set myconn = SetMyConn
Resume Next
Case Else
Debug.Print erADO.Number & " " & erADO.description & " " & erADO.SQLState & " " & erADO.NativeError
Resume Exit_cmdFindJob_Click
End Select
Next
If Err.Number = 3709 Then
Call SetMyConn
Resume Next
Else
MsgBox Err.Number & " " & Err.description
Resume Exit_cmdFindJob_Click
End If
End Sub
Private Sub cmdFindJob_Click()
On Error GoTo Err_Handler
strJobno = Me.Jobno
strDept = Me.dept
Dim rst As ADODB.Recordset
Dim myconn As ADODB.Connection
Set myconn = SetMyConn
strSql = "SELECT jobid FROM usijobs as j " & _
"WHERE j.jobno = '" & strJobno & "' AND dept = '" & strDept & "';"
Set rst = New ADODB.Recordset
rst.Open strSql, myconn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Not rst.EOF Or Not rst.BOF Then
rst.MoveFirst
lngJobSetupOpenargs = rst!jobid
strFrmName = "USIJobSetup"
If Application.CurrentProject.AllForms(strFrmName).IsLoaded Then
Debug.Print "Form is loaded"
Forms.Item(strFrmName).SetFocus
' DoCmd.Close acForm, "USIFindJobSetup", acSaveNo
Else
DoCmd.OpenForm strFrmName, acNormal, , , acFormEdit, acWindowNormal, rst!jobid
End If
Dim jsform As Form
Set jsform = Application.Forms(strFrmName)
jsform.Controls("Jobno") = strJobno
jsform.Controls("Dept") = strDept
DoCmd.Close acForm, "USIFindJob", acSaveNo
Else
MsgBox "No Job record exisits for the criteria you have provided.", vbOKOnly, "NO JOB RECORD"
End If
rst.Close
Set rst = Nothing
myconn.Close
Set myconn = Nothing
Exit_cmdFindJob_Click:
Exit Sub
Err_Handler:
Dim erADO As ADODB.Error
For Each erADO In myconn.Errors
Select Case erADO.NativeError
Case 1045
Debug.Print erADO.Number & " " & erADO.description & " " & erADO.SQLState & " " & erADO.NativeError
Set myconn = SetMyConn
Resume Next
Case Else
Debug.Print erADO.Number & " " & erADO.description & " " & erADO.SQLState & " " & erADO.NativeError
Resume Exit_cmdFindJob_Click
End Select
Next
If Err.Number = 3709 Then
Call SetMyConn
Resume Next
Else
MsgBox Err.Number & " " & Err.description
Resume Exit_cmdFindJob_Click
End If
End Sub