G
Guest
Hello John,
Some time ago (12/3/2004) you described how to select a Postcode & State
depending on the suburb selected.
I have been trying to do this for a while now but i am not having any luck,
your help would be most apperiated.
I have a table, tblPostcodes with the fields, Postcode, State, Suburb.
I have on my Customers form 3 combo boxes named, cboSuburb, cboState,
cboPostcode, the Control Source for each cbo is its field in the Customers
table. eg: the control source for cboSuburb is the field Suburb in the
Customers table.
I have connected the code below to the AfterUpdate events of the respective
combo's.
Private Sub cboSuburb_AfterUpdate()
On Error GoTo PROC_ERR
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim iCount As Integer
Dim strSQL As String
' Set default to all postcodes
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes ORDER BY
Postcode;"
' If a suburb is selected, limit the postcode to those in the selected
' suburb; if the suburb has only one postcode, just set it to that value
If Not IsNull(cboSuburb) Then
Set db = CurrentDb
strSQL = "SELECT Postcode, State FROM tblPostcodes WHERE " & _
"tblPostcodes.Suburb=" & Chr(34) & cboSuburb & Chr(34) & _
IIf(IsNull(Me.cboState), " ", Me.cboState) & " AND tblPostcodes.State =
'" _
& Me.cboState & "' ORDER BY Suburb;"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
rs.MoveLast
iCount = rs.RecordCount
Select Case iCount
Case 0
' do nothing if this suburb isn't in the postcode table
Case 1
' If there's just one suburb/postcode, set postcode and state
' to the selected one
Me.cboPostcode = rs!Postcode
Me.cboState = rs!State
Case Else
' to the selected suburb's postcodes
Me.cboPostcode.RowSource = strSQL
End Select
rs.Close
Set rs = Nothing
Set db = Nothing
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in cboSuburb_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
Private Sub cboState_AfterUpdate()
On Error GoTo PROC_ERR
If IsNull(cboState) Then
'State has been cleared, set postcode and suburb combos to all
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State " _
& "FROM tblPostcodes ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes" _
& "ORDER BY Postcode;"
Else
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State" _
& "FROM tblPostcodes WHERE tblPostcodes.State='" & cboState & "'" _
& "ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes" _
& "WHERE tblPostcodes.State='" & cboState & "' ORDER BY Postcode;"
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error" & Err.Number & " in cboState_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
Private Sub cboPostcode_AfterUpdate()
On Error GoTo PROC_ERR
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim iCount As Integer
Dim strSQL As String
' Set default to all suburbs
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State FROM " _
& "tblPostcodes ORDER BY Suburb;"
If Not IsNull(cboPostcode) Then
Set db = CurrentDb
strSQL = "SELECT DISTINCT Suburb, State FROM tblPostcodes" _
& "WHERE tblPostcodes.Postcode='" & cboPostcode & "' ORDER BY Suburb;"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
iCount = rs.RecordCount
Select Case iCount
Case 0
'do nothing
Case 1
Me.cboSuburb = rs!Suburb
Me.cboState = rs!State
End Select
rs.Close
Set rs = Nothing
Set db = Nothing
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error" & Err.Number & "in cboPostcode_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
Private Sub Form_Current()
On Error GoTo PROC_ERR
If IsNull(Me.cboState) Then
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State " _
& "FROM tblPostcodes ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode " _
& "FROM tblPostcodes ORDER BY Postcode;"
Else
Me.cboSuburb.RowSource = "SELECT DISTINCT City, State " _
& "FROM tblPostcodes WHERE [State]='" & cboState & "' ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes " _
& "WHERE [State]='" & cboState & "' ORDER BY Postcode;"
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in Form_Current:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
The problem i am having is when i select a suburb from the combo i get an
error message,
Error 3021 in cboSuburb_AfterUpdate: No Current Record
Can you see where i have gone wrong
regards David
Some time ago (12/3/2004) you described how to select a Postcode & State
depending on the suburb selected.
I have been trying to do this for a while now but i am not having any luck,
your help would be most apperiated.
I have a table, tblPostcodes with the fields, Postcode, State, Suburb.
I have on my Customers form 3 combo boxes named, cboSuburb, cboState,
cboPostcode, the Control Source for each cbo is its field in the Customers
table. eg: the control source for cboSuburb is the field Suburb in the
Customers table.
I have connected the code below to the AfterUpdate events of the respective
combo's.
Private Sub cboSuburb_AfterUpdate()
On Error GoTo PROC_ERR
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim iCount As Integer
Dim strSQL As String
' Set default to all postcodes
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes ORDER BY
Postcode;"
' If a suburb is selected, limit the postcode to those in the selected
' suburb; if the suburb has only one postcode, just set it to that value
If Not IsNull(cboSuburb) Then
Set db = CurrentDb
strSQL = "SELECT Postcode, State FROM tblPostcodes WHERE " & _
"tblPostcodes.Suburb=" & Chr(34) & cboSuburb & Chr(34) & _
IIf(IsNull(Me.cboState), " ", Me.cboState) & " AND tblPostcodes.State =
'" _
& Me.cboState & "' ORDER BY Suburb;"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
rs.MoveLast
iCount = rs.RecordCount
Select Case iCount
Case 0
' do nothing if this suburb isn't in the postcode table
Case 1
' If there's just one suburb/postcode, set postcode and state
' to the selected one
Me.cboPostcode = rs!Postcode
Me.cboState = rs!State
Case Else
' to the selected suburb's postcodes
Me.cboPostcode.RowSource = strSQL
End Select
rs.Close
Set rs = Nothing
Set db = Nothing
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in cboSuburb_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
Private Sub cboState_AfterUpdate()
On Error GoTo PROC_ERR
If IsNull(cboState) Then
'State has been cleared, set postcode and suburb combos to all
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State " _
& "FROM tblPostcodes ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes" _
& "ORDER BY Postcode;"
Else
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State" _
& "FROM tblPostcodes WHERE tblPostcodes.State='" & cboState & "'" _
& "ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes" _
& "WHERE tblPostcodes.State='" & cboState & "' ORDER BY Postcode;"
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error" & Err.Number & " in cboState_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
Private Sub cboPostcode_AfterUpdate()
On Error GoTo PROC_ERR
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim iCount As Integer
Dim strSQL As String
' Set default to all suburbs
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State FROM " _
& "tblPostcodes ORDER BY Suburb;"
If Not IsNull(cboPostcode) Then
Set db = CurrentDb
strSQL = "SELECT DISTINCT Suburb, State FROM tblPostcodes" _
& "WHERE tblPostcodes.Postcode='" & cboPostcode & "' ORDER BY Suburb;"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
iCount = rs.RecordCount
Select Case iCount
Case 0
'do nothing
Case 1
Me.cboSuburb = rs!Suburb
Me.cboState = rs!State
End Select
rs.Close
Set rs = Nothing
Set db = Nothing
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error" & Err.Number & "in cboPostcode_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
Private Sub Form_Current()
On Error GoTo PROC_ERR
If IsNull(Me.cboState) Then
Me.cboSuburb.RowSource = "SELECT DISTINCT Suburb, State " _
& "FROM tblPostcodes ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode " _
& "FROM tblPostcodes ORDER BY Postcode;"
Else
Me.cboSuburb.RowSource = "SELECT DISTINCT City, State " _
& "FROM tblPostcodes WHERE [State]='" & cboState & "' ORDER BY Suburb;"
Me.cboPostcode.RowSource = "SELECT Postcode FROM tblPostcodes " _
& "WHERE [State]='" & cboState & "' ORDER BY Postcode;"
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in Form_Current:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
_____________________________________________________________________
The problem i am having is when i select a suburb from the combo i get an
error message,
Error 3021 in cboSuburb_AfterUpdate: No Current Record
Can you see where i have gone wrong
regards David