Error Handler, Compile code
---
Hi Stacey,
this needs another statement (End If):
If Len(Trim(strWhere3)) > 0 Then
strWhere3 = strWhere3 & " AND "
End If
Which statement is causing the problem? I see you have an error handler
at the bottom, but you have not set the procedure up to go there...
While I am developing, I like to make the error handler go to the line
that caused the problem so I can see where it is. Stop will stop the
code and Resume goes back to the offending line. When code Stops, press
F8 to execute one statement at a time.
Add an error handler to your code
put this at the top of your program, right after the procedure
declaration (skip a line first for better readability)
'~~~~~~~~~~~~~~~~~~~~~~
'set up Error Handler
On Error GoTo Proc_Err
'~~~~~~~~~~~~~~~~~~~~~~
... then your statements
'~~~~~~~~~~~~~~~~~~~~~~
put this at the end of the program
'~~~~~~~~~~~~~~~~~~~~~~
Proc_Exit:
On Error Resume Next
'release object variables if any
Exit Function
Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " ProcedureName"
'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
Resume Proc_Exit
'~~~~~~~~~~~~~~~~~~~~~~
where
ProcedureName is the name of your procedure so you can identify what
code the problem is in when you see the error
The line labels do not matter (Proc_Exit:, Proc_Err
, I like to use the
same ones all the time -- they only have to be unique within a procedure.
'~~~~~~~~~ Compile ~~~~~~~~~
Whenever you change code or references, your should always compile
before executing.
from the menu in a module window: Debug, Compile
fix any errors on the yellow highlighted lines
keep compiling until nothing happens (this is good!)
***********************************************
Warm Regards,
Crystal
*
have an awesome day
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
Stacey wrote:
Hi Crystal...thanks for helping! Ok, I followed your suggestions, and now I
get a data mismatch...any recommendations? see below for the code.
Private Sub cmdEntitlementReport_Click()
Dim strReport As String
Dim strWhere1 As String
Dim varItem As Variant
Dim strWhere2 As String
Dim strWhere3 As String
Dim strDelim As String
strReport = "entitlements"
strDelim = ""
With Me.Bkname
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere1 = strWhere1 & strDelim & _
.ItemData(varItem) & strDelim & ","
End If
Next
End With
lngLen = Len(strWhere1) - 1
If lngLen > 0 Then
strWhere1 = "[Bank_Number] IN (" & Left$(strWhere1, lngLen) & ")"
End If
With Me.Conumber
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere2 = strWhere2 & strDelim & _
.ItemData(varItem) & strDelim & ","
End If
Next
End With
lngLen = Len(strWhere2) - 1
If lngLen > 0 Then
strWhere2 = "[companyno] IN (" & Left$(strWhere2, lngLen) & ")"
End If
If Len(Trim(strWhere1)) > 0 Then
strWhere3 = strWhere1
End If
If Len(Trim(strWhere2)) > 0 Then
If Len(Trim(strWhere3)) > 0 Then
strWhere3 = strWhere3 & " AND "
strWhere3 = strWhere3 & strWhere2
End If
End If
DoCmd.OpenReport strReport, acViewPreview, , strWhere3
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"cmdEntitlementReport_Click"
End If
End Sub
:
Initialize variables
---
Hi Stacey,
what is the value of strDelim ? you have not initialized it.
for text:
strDelim = "'"
for numbers:
strDelim = ""
for dates:
strDelim = "#"
you also need to initalize your string variables:
strWhere1 = ""
strWhere2 = ""
strWhere3 = ""
it is also better to test the length of a string instead of the value...
instead of
If strWhere1 = "" Then
strWhere3 = strWhere2
ElseIf strWhere2 = "" Then
strWhere3 = strWhere1
Else
strWhere3 = strWhere1 & " AND " & strWhere2
End If
try this:
If len(trim(strWhere1)) > 0 Then
strWhere3 = strWhere1
end if
If len(trim(strWhere2)) > 0 Then
If len(trim(strWhere3)) > 0 Then
strWhere3 = strWhere3 & " AND "
strWhere3 = strWhere3 & strWhere2
end if
~~~
and one last note:
it is a good idea to change the NAME property of controls before you use
them in code.
for instance:
Command27 --> cmdEntitlementReport
then, the procedure devclaration line should change from
Private Sub Command27_Click()
to
Private Sub cmdEntitlementReport_Click()
Warm Regards,
Crystal
*
have an awesome day
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
Stacey wrote:
I recently recieved some help from Allen regarding another issue and I am
trying oto incorporate the code he gave me to perform another task...however
it doesn't seem to work. Can someone please take a look at the code and tell
me what I am doing wrong. Thanks in advance!
Private Sub Command27_Click()
Dim strReport As String
Dim strWhere1 As String
Dim varItem As Variant
Dim strWhere2 As String
Dim strWhere3 As String
Dim strDelim As String
strReport = "entitlements"
With Me.List12
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere1 = strWhere1 & strDelim & _
.ItemData(varItem) & strDelim & ","
End If
Next
End With
lngLen = Len(strWhere1) - 1
If lngLen > 0 Then
strWhere1 = "[Bank_Number] IN (" & Left$(strWhere1, lngLen) & ")"
End If
With Me.List21
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere2 = strWhere2 & strDelim & _
.ItemData(varItem) & strDelim & ","
End If
Next
End With
lngLen = Len(strWhere2) - 1
If lngLen > 0 Then
strWhere2 = "[companyno] IN (" & Left$(strWhere2, lngLen) & ")"
End If
If strWhere1 = "" Then
strWhere3 = strWhere2
ElseIf strWhere2 = "" Then
strWhere3 = strWhere1
Else
strWhere3 = strWhere1 & " AND " & strWhere2
End If
DoCmd.OpenReport strReport, acViewPreview, , strWhere3
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"Command27_Click"
End If
End Sub