TheGaffer8
11-19-2008, 05:37 AM
Hi All,
I've got a bit of a problem and I wondered if you might be able to help...
I've got a form called frmOrdersByCustomer whose record source is tblCustomers. Each customer has an alerts function so that we can have a flash up on screen of any really important information before we go into any of the main details. To get this form up on screen I have the following code:
Private Sub Form_Current()
If IsNull(Me![CustomerID]) Then
DoCmd.GoToControl "ProprietorAddress"
End If
' Opens frmAlerts form with a filter based on Ticket ID and an Alert where the DateIssued has already passed, but where the DateExpires has not
Dim strSQLAlerts As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
strSQLAlerts = "SELECT tblAlerts.* " & _
"FROM tblAlerts " & _
"WHERE (((tblAlerts.DateIssued)<=Now()) AND ((tblAlerts.DateExpires)>=Now()) AND ((tblAlerts.CustomerID)=" & Form_frmOrdersByCustomer.CustomerID & ") AND ((tblAlerts.LogicalStatusID)=1));"
Debug.Print strSQLAlerts
rst.Open strSQLAlerts, CurrentProject.Connection
If Not rst.EOF Then
DoCmd.OpenForm "frmAlerts", , , "[CustomerID]=" & [Forms]![frmOrdersByCustomer]![CustomerID] & " AND [DateIssued] <= Now() And [DateExpires] >= Now() And [LogicalStatusID] = 1", acFormEdit
End If
rst.Close
' Checks the status of the displayed Customer's account and adjusts colours accordingly
AccStatusChange
Me.tabOrderSummary.SetFocus
' Resets the caption on button cmdAllowEdits
' Locks records to prevent user changes
Me.cmdAllowEdits.Caption = "Edit This Customer"
LockCustomer
End Sub
Now it all works perfectly fine... and the form for Alerts is displayed if there are any valid alerts...
Here's where it gets very strange...
On the same form (frmOrdersByCustomer) I've got a number of search criteria to be able to set a new recordsource for the form based on whatever the user wants to narrow the records down to. To do this, I have code:
Private Sub cmdSearch_Click()
Dim strSQLHead As String
Dim strSQLWhere As String
Dim strSQLOrderBy As String
Dim strSQL As String
Dim strJoin As String
strJoin = " AND "
strSQLHead = "SELECT * FROM tblCustomers "
If Len(Me.txtCustomerIDSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
strSQLWhere = strSQLWhere & "[CustomerID]=" & Me.txtCustomerIDSearch
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.txtProprietorNameSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
If (Me.chkLikeProprietor) Then
strSQLWhere = strSQLWhere & "[ProprietorName] Like '*" & Me.txtProprietorNameSearch & "*'"
Else
strSQLWhere = strSQLWhere & "[ProprietorName] = '" & Me.txtProprietorNameSearch & "'"
End If
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.txtPremiseNameSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
If (Me.chkLikePremise) Then
strSQLWhere = strSQLWhere & "[PremiseName] Like '*" & Me.txtPremiseNameSearch & "*'"
Else
strSQLWhere = strSQLWhere & "[PremiseName] = '" & Me.txtPremiseNameSearch & "'"
End If
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.txtPostCodeSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
strSQLWhere = strSQLWhere & "[PostCode]= '" & Me.txtPostCodeSearch & "'"
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.cboAuthAreaSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
strSQLWhere = strSQLWhere & "[AuthArea] = '" & Me.cboAuthAreaSearch & "'"
strSQLWhere = strSQLWhere & strJoin
End If
If Len(strSQLWhere) Then
strSQLWhere = Left$(strSQLWhere, Len(strSQLWhere) - (Len(strJoin) - 1))
End If
strSQLOrderBy = "ORDER BY "
strSQLOrderBy = strSQLOrderBy & "[CustomerID] DESC"
strSQL = strSQLHead & strSQLWhere & strSQLOrderBy
Debug.Print strSQL
Me.RecordSource = strSQL
End Sub
Now this too works absolutely fine...
EXCEPT for when the first record displayed on the main form after using the search feature, ALSO has a valid alert.... Instead of getting all the Alert information, all I get is #Name? in every box on the frmAlerts form...
Strangely, if I change to a different one and then back, all the details are there and in full... so I guess it's something to do with when I open the recordset on the search button, whilst the On_Current part is also opening a recordset on the alerts form...
The Form_Current code, sets the recordsource for frmAlerts based on all whether or not there are any valid alerts for the Customer displayed on frmOrdersByCustomer (the Form_Current code is run from the frmOrdersByCustomer form).
The cmdSearch button sets the recordsource for the frmOrdersByCustomer, but naturally since the routine would have affected the Form_Current, this code will run too... And even though the recordsource is being changed for two different forms, they still seem to conflict???
The fields with #Name? are ONLY on the frmAlerts form... all the records on the frmOrdersByCustomer form appear correctly...
Problem is, I don't know enough about opening the recordsets in the first place to understand what exactly was happening.
I've attached a sample of forms and code if anyone can have a look and see what they think...
Any ideas?
I've got a bit of a problem and I wondered if you might be able to help...
I've got a form called frmOrdersByCustomer whose record source is tblCustomers. Each customer has an alerts function so that we can have a flash up on screen of any really important information before we go into any of the main details. To get this form up on screen I have the following code:
Private Sub Form_Current()
If IsNull(Me![CustomerID]) Then
DoCmd.GoToControl "ProprietorAddress"
End If
' Opens frmAlerts form with a filter based on Ticket ID and an Alert where the DateIssued has already passed, but where the DateExpires has not
Dim strSQLAlerts As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
strSQLAlerts = "SELECT tblAlerts.* " & _
"FROM tblAlerts " & _
"WHERE (((tblAlerts.DateIssued)<=Now()) AND ((tblAlerts.DateExpires)>=Now()) AND ((tblAlerts.CustomerID)=" & Form_frmOrdersByCustomer.CustomerID & ") AND ((tblAlerts.LogicalStatusID)=1));"
Debug.Print strSQLAlerts
rst.Open strSQLAlerts, CurrentProject.Connection
If Not rst.EOF Then
DoCmd.OpenForm "frmAlerts", , , "[CustomerID]=" & [Forms]![frmOrdersByCustomer]![CustomerID] & " AND [DateIssued] <= Now() And [DateExpires] >= Now() And [LogicalStatusID] = 1", acFormEdit
End If
rst.Close
' Checks the status of the displayed Customer's account and adjusts colours accordingly
AccStatusChange
Me.tabOrderSummary.SetFocus
' Resets the caption on button cmdAllowEdits
' Locks records to prevent user changes
Me.cmdAllowEdits.Caption = "Edit This Customer"
LockCustomer
End Sub
Now it all works perfectly fine... and the form for Alerts is displayed if there are any valid alerts...
Here's where it gets very strange...
On the same form (frmOrdersByCustomer) I've got a number of search criteria to be able to set a new recordsource for the form based on whatever the user wants to narrow the records down to. To do this, I have code:
Private Sub cmdSearch_Click()
Dim strSQLHead As String
Dim strSQLWhere As String
Dim strSQLOrderBy As String
Dim strSQL As String
Dim strJoin As String
strJoin = " AND "
strSQLHead = "SELECT * FROM tblCustomers "
If Len(Me.txtCustomerIDSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
strSQLWhere = strSQLWhere & "[CustomerID]=" & Me.txtCustomerIDSearch
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.txtProprietorNameSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
If (Me.chkLikeProprietor) Then
strSQLWhere = strSQLWhere & "[ProprietorName] Like '*" & Me.txtProprietorNameSearch & "*'"
Else
strSQLWhere = strSQLWhere & "[ProprietorName] = '" & Me.txtProprietorNameSearch & "'"
End If
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.txtPremiseNameSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
If (Me.chkLikePremise) Then
strSQLWhere = strSQLWhere & "[PremiseName] Like '*" & Me.txtPremiseNameSearch & "*'"
Else
strSQLWhere = strSQLWhere & "[PremiseName] = '" & Me.txtPremiseNameSearch & "'"
End If
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.txtPostCodeSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
strSQLWhere = strSQLWhere & "[PostCode]= '" & Me.txtPostCodeSearch & "'"
strSQLWhere = strSQLWhere & strJoin
End If
If Len(Me.cboAuthAreaSearch & vbNullString) Then
If Len(strSQLWhere) = 0 Then
strSQLWhere = "WHERE "
End If
strSQLWhere = strSQLWhere & "[AuthArea] = '" & Me.cboAuthAreaSearch & "'"
strSQLWhere = strSQLWhere & strJoin
End If
If Len(strSQLWhere) Then
strSQLWhere = Left$(strSQLWhere, Len(strSQLWhere) - (Len(strJoin) - 1))
End If
strSQLOrderBy = "ORDER BY "
strSQLOrderBy = strSQLOrderBy & "[CustomerID] DESC"
strSQL = strSQLHead & strSQLWhere & strSQLOrderBy
Debug.Print strSQL
Me.RecordSource = strSQL
End Sub
Now this too works absolutely fine...
EXCEPT for when the first record displayed on the main form after using the search feature, ALSO has a valid alert.... Instead of getting all the Alert information, all I get is #Name? in every box on the frmAlerts form...
Strangely, if I change to a different one and then back, all the details are there and in full... so I guess it's something to do with when I open the recordset on the search button, whilst the On_Current part is also opening a recordset on the alerts form...
The Form_Current code, sets the recordsource for frmAlerts based on all whether or not there are any valid alerts for the Customer displayed on frmOrdersByCustomer (the Form_Current code is run from the frmOrdersByCustomer form).
The cmdSearch button sets the recordsource for the frmOrdersByCustomer, but naturally since the routine would have affected the Form_Current, this code will run too... And even though the recordsource is being changed for two different forms, they still seem to conflict???
The fields with #Name? are ONLY on the frmAlerts form... all the records on the frmOrdersByCustomer form appear correctly...
Problem is, I don't know enough about opening the recordsets in the first place to understand what exactly was happening.
I've attached a sample of forms and code if anyone can have a look and see what they think...
Any ideas?