VBA Compilation Error - Please Review

G

Guest

Hello all,
Below is a VBA command that I created to verify that a previous value has
been entered and to open a new form if it has. I have looked this thing over
for about an hour and still no luck. I am fairly new at VBA and advice or
critiques will be much appreciated. Many Thanks, Adam Kemp

Private Sub Custom_ID_Click()
IIf(IsNull(Forms![Invoice Entry]![Invoice ID]), MsgBox("Please enter and
Invoice # before proceeding." _
& Chr(13) & Chr(10) _
& "This will insure data integrity." _
& Chr(13) & Chr(10) _
& "Press OK to return to previous screen", _
vbOK, "Insufficient Data"), DoCmd.OpenForm (Form![SelectInvEncu#1],
acNormal, , [Custom ID] = [Forms]![Invoice
Entry]![EntryLedgerListing].[Form]![Custom ID], acFormEdit, acDialog) =
vbOK)

End If
End Sub
 
A

Albert D. Kallal

As a general rule, you don't need, nor use iif in code, but only in
expressions.

lets try and clean this code up...since as you have it...it is NEAR
impossible to read....


So your code could be written as:

Private Sub Custom_ID_Click()

Dim strwhere As String

If IsNull(Me![Invoice id]) Then

MsgBox "Plase enter and invoice # before proceeding" & vbCrLf & _
"This will insure data integrity." & vbCrLf & _
"Press OK to return to previous screen", _
vbOK, "Insufficient Data"
Else

strwhere = "[custom id] = " & _
Me![EntryLedgerListing].[Form]![Custom id]

DoCmd.OpenForm "SelectInvEncu#1", acNormal, _
, strwhere, acFormEdit, acDialog
End If

End Sub

Now, in a few places I used the "_" (line continuation) character to make
this easer to read..and the newsgroup does
tend to "wrap" the text...and mess things up....so, I use the "_" to avoid
this..

Note how much easier the code is to read.....
 
P

Perry

I am fairly new at VBA

Don't try to force multiple commands into VBA oneliners, especially if y're
new to VBA :)
If you have to service the database in the future, you won't like what you
did previously with these oneliners ...

If you were to break yr code into readable bits and pieces, you will resolve
this yrself.
So break it down.

Kindly repost, if you have difficulties after that.

Krgrds,
Perry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top