L
Lee Taylor-Vaughan
Hello group,
I have tried to get the following code to work, but i keep getting the
follwing error - upon the form opening it get "error number 13 : type
mismatch"
also my CountRecords Private Sub does not work, and neither does the
SearchNow private module... what i am doing wrong?
I think it is related to the sql code.. please explain.. Thanks in advance
(code below)
CODE>>>
Option Compare Database
Option Explicit
'sql statements used to polpulate lstbox (initial sql w/out order by &
where)
Const cstrOldSQL1 = "SELECT tblActivity.ControlNumberID AS CNumber, "
Const cstrOldSQL2 = "tblActivity.DateDispatch AS [Disp Date], "
Const cstrOldSQL3 = "tblActivity.DispatchType AS Type,
tblActivity.TimeDispatch AS [Disp Time], "
Const cstrOldSQL4 = "tblActivity.IncidentLocationCity AS Location, "
Const cstrOldSQL5 = "[PatientLastName] & '" & ", ' & [patientfirstname] AS
[Pt Name] "
Const cstrOldSQL6 = "FROM tblActivity INNER JOIN tblPatients "
Const cstrOldSQL7 = "ON
tblActivity.ControlNumberID=tblPatients.ControlNumberID"
'concatenates above constants for form one...
Const cstrOldSQLa = cstrOldSQL1 & cstrOldSQL2 & cstrOldSQL3 & cstrOldSQL4 &
cstrOldSQL5 & cstrOldSQL6
Const cstrOldSQL = cstrOldSQLa & cstrOldSQL7
'default sql construct
Const cstrDefaultOrder = " ORDER BY tblActivity.datedispatch DESC ,
tblActivity.TimeDispatch;"
Const cstrDefaultSQL = cstrOldSQL & cstrDefaultOrder
Private Sub Command274_Click()
CountRecords
End Sub
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
cmdReset_Click
DoCmd.Restore
Exit_Form_Open:
Exit Sub
Err_Form_Open:
MsgBox Err.Number & Err.Description
Resume Exit_Form_Open
End Sub
Private Sub cmdReset_Click()
' Assign put the new SQL value into the recordsource
Me.lstDispMain.RowSource = cstrDefaultSQL
Me.lstDispMain.Requery
Me.Refresh
Me.Repaint
CountRecords
End Sub
Private Sub CountRecords()
On Error GoTo Err_Clear_Click
Dim lngCount As Integer
Dim dbCurrent As Database
Dim rsData As Recordset
Set dbCurrent = CurrentDb
Set rsData = CurrentDb.OpenRecordset(Me.lstDispMain.RowSource)
lngCount = 0
With rsData
.MoveLast
.MoveFirst
Do While rsData.EOF = False
.MoveNext
lngCount = lngCount + 1
Debug.Print lngCount
Loop
End With
Forms!frmDispatchMain.txtTotal = lngCount
Exit_Clear_Click:
Exit Sub
Err_Clear_Click:
Select Case Err.Number
Case Is = 3021 'no records in set
MsgBox "There are no Dispatches for your selection"
Me.lstDispMain.RowSource = cstrDefaultSQL
Exit Sub
Case Else
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error
Message, gulp...!"
Exit Sub
End Select
Set dbCurrent = Nothing
Set rsData = Nothing
End Sub
Private Sub cmdSQL_Click()
MsgBox Me.lstDispMain.RowSource, vbInformation + vbOKOnly, "SQL Statement
used for: " & Me.Name
End Sub
Public Sub cmdSearchNow_Click()
On Error GoTo Err_cmdSearch_Click
Dim strNewSQL As String ' variable to hold the new SQL string for the
RecordSource
Dim dbCurrent As Database
Dim rsData As Recordset
Dim lngCount As Integer
Select Case optSearch
Case 1
If IsNull(Forms!frmDispatchMain!txtFind) Then ' Search by Patient Last
Name
MsgBox "Please enter a name.", , "Data Required"
Exit Sub
ElseIf Not IsNull(Me.txtFind) Then
strNewSQL = cstrOldSQL & " Where [Pt Name] Like '" &
Forms!frmDispatchMain![txtFind].Value & "*' Order by [Pt Name];"
End If
Case 2
If IsNull(txtFind) Then ' Search by control number
MsgBox "Please enter a Control Number (Last 6).", , "Data Required"
Exit Sub
ElseIf Not IsNull(txtFind) Then
strNewSQL = cstrOldSQL & " Where tblActivity.ControlNumberID Like '" &
Forms!frmDispatchMain![txtFind].Value & "*' Order by
tblActivity.ControlNumberID;"
End If
Case 3
If IsNull(txtFind) Then ' Search by Date
MsgBox "Please enter a Date in mm/dd/yy format", , "Data Required"
Exit Sub
ElseIf Not IsNull(txtFind) Then
strNewSQL = cstrOldSQL & " Where tblActivity.datedispatch = #" &
Forms!frmDispatchMain![txtFind].Value & "# Order by
tblActivity.datedispatch,tblActivity.TimeDIspatch;"
End If
End Select
Forms!frmDispatchMain.lstDispMain = strNewSQL
Forms!frmDispatchMain.lstDispMain.Requery
Forms!frmDispatchMain.Refresh
Forms!frmDispatchMain.Repaint
CountRecords
' Assign put the new SQL value into the recordsource
Exit_cmdSearch_Click:
Exit Sub
Err_cmdSearch_Click:
If Err.Number = 3021 Then
MsgBox "There are no Missions for your selection; please retry your
search...", , "Search"
strNewSQL = cstrDefaultSQL
Forms!frmDispatchMain.lstDispMain.RowSource = strNewSQL
Forms!frmDispatchMain.Requery
Else
MsgBox Err.Number & Err.Description
Resume Exit_cmdSearch_Click
End If
Resume Exit_cmdSearch_Click
End Sub
I have tried to get the following code to work, but i keep getting the
follwing error - upon the form opening it get "error number 13 : type
mismatch"
also my CountRecords Private Sub does not work, and neither does the
SearchNow private module... what i am doing wrong?
I think it is related to the sql code.. please explain.. Thanks in advance
(code below)
CODE>>>
Option Compare Database
Option Explicit
'sql statements used to polpulate lstbox (initial sql w/out order by &
where)
Const cstrOldSQL1 = "SELECT tblActivity.ControlNumberID AS CNumber, "
Const cstrOldSQL2 = "tblActivity.DateDispatch AS [Disp Date], "
Const cstrOldSQL3 = "tblActivity.DispatchType AS Type,
tblActivity.TimeDispatch AS [Disp Time], "
Const cstrOldSQL4 = "tblActivity.IncidentLocationCity AS Location, "
Const cstrOldSQL5 = "[PatientLastName] & '" & ", ' & [patientfirstname] AS
[Pt Name] "
Const cstrOldSQL6 = "FROM tblActivity INNER JOIN tblPatients "
Const cstrOldSQL7 = "ON
tblActivity.ControlNumberID=tblPatients.ControlNumberID"
'concatenates above constants for form one...
Const cstrOldSQLa = cstrOldSQL1 & cstrOldSQL2 & cstrOldSQL3 & cstrOldSQL4 &
cstrOldSQL5 & cstrOldSQL6
Const cstrOldSQL = cstrOldSQLa & cstrOldSQL7
'default sql construct
Const cstrDefaultOrder = " ORDER BY tblActivity.datedispatch DESC ,
tblActivity.TimeDispatch;"
Const cstrDefaultSQL = cstrOldSQL & cstrDefaultOrder
Private Sub Command274_Click()
CountRecords
End Sub
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
cmdReset_Click
DoCmd.Restore
Exit_Form_Open:
Exit Sub
Err_Form_Open:
MsgBox Err.Number & Err.Description
Resume Exit_Form_Open
End Sub
Private Sub cmdReset_Click()
' Assign put the new SQL value into the recordsource
Me.lstDispMain.RowSource = cstrDefaultSQL
Me.lstDispMain.Requery
Me.Refresh
Me.Repaint
CountRecords
End Sub
Private Sub CountRecords()
On Error GoTo Err_Clear_Click
Dim lngCount As Integer
Dim dbCurrent As Database
Dim rsData As Recordset
Set dbCurrent = CurrentDb
Set rsData = CurrentDb.OpenRecordset(Me.lstDispMain.RowSource)
lngCount = 0
With rsData
.MoveLast
.MoveFirst
Do While rsData.EOF = False
.MoveNext
lngCount = lngCount + 1
Debug.Print lngCount
Loop
End With
Forms!frmDispatchMain.txtTotal = lngCount
Exit_Clear_Click:
Exit Sub
Err_Clear_Click:
Select Case Err.Number
Case Is = 3021 'no records in set
MsgBox "There are no Dispatches for your selection"
Me.lstDispMain.RowSource = cstrDefaultSQL
Exit Sub
Case Else
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error
Message, gulp...!"
Exit Sub
End Select
Set dbCurrent = Nothing
Set rsData = Nothing
End Sub
Private Sub cmdSQL_Click()
MsgBox Me.lstDispMain.RowSource, vbInformation + vbOKOnly, "SQL Statement
used for: " & Me.Name
End Sub
Public Sub cmdSearchNow_Click()
On Error GoTo Err_cmdSearch_Click
Dim strNewSQL As String ' variable to hold the new SQL string for the
RecordSource
Dim dbCurrent As Database
Dim rsData As Recordset
Dim lngCount As Integer
Select Case optSearch
Case 1
If IsNull(Forms!frmDispatchMain!txtFind) Then ' Search by Patient Last
Name
MsgBox "Please enter a name.", , "Data Required"
Exit Sub
ElseIf Not IsNull(Me.txtFind) Then
strNewSQL = cstrOldSQL & " Where [Pt Name] Like '" &
Forms!frmDispatchMain![txtFind].Value & "*' Order by [Pt Name];"
End If
Case 2
If IsNull(txtFind) Then ' Search by control number
MsgBox "Please enter a Control Number (Last 6).", , "Data Required"
Exit Sub
ElseIf Not IsNull(txtFind) Then
strNewSQL = cstrOldSQL & " Where tblActivity.ControlNumberID Like '" &
Forms!frmDispatchMain![txtFind].Value & "*' Order by
tblActivity.ControlNumberID;"
End If
Case 3
If IsNull(txtFind) Then ' Search by Date
MsgBox "Please enter a Date in mm/dd/yy format", , "Data Required"
Exit Sub
ElseIf Not IsNull(txtFind) Then
strNewSQL = cstrOldSQL & " Where tblActivity.datedispatch = #" &
Forms!frmDispatchMain![txtFind].Value & "# Order by
tblActivity.datedispatch,tblActivity.TimeDIspatch;"
End If
End Select
Forms!frmDispatchMain.lstDispMain = strNewSQL
Forms!frmDispatchMain.lstDispMain.Requery
Forms!frmDispatchMain.Refresh
Forms!frmDispatchMain.Repaint
CountRecords
' Assign put the new SQL value into the recordsource
Exit_cmdSearch_Click:
Exit Sub
Err_cmdSearch_Click:
If Err.Number = 3021 Then
MsgBox "There are no Missions for your selection; please retry your
search...", , "Search"
strNewSQL = cstrDefaultSQL
Forms!frmDispatchMain.lstDispMain.RowSource = strNewSQL
Forms!frmDispatchMain.Requery
Else
MsgBox Err.Number & Err.Description
Resume Exit_cmdSearch_Click
End If
Resume Exit_cmdSearch_Click
End Sub