Topic: VBA Code in Access
someguy1313's photo
Mon 11/10/08 04:44 AM
I am new to VBA does anyone know how this SHOULD have been written... I know every IF needs an END IF and the word OR does not seem to work so how would I cover all the below... and writing an IF for each does not work.

Private Sub cmbInternalStatus_Exit(Cancel As Integer)

If cmbInternalStatus = "Approved" And cmbWebStatus = "Approved" Then
Cancel = False
If cmbInternalStatus = "Closed (Other)" And cmbWebStatus = "Closed (Other)" Then
Cancel = False
If cmbInternalStatus = "Director Review" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
If cmbInternalStatus = "Disapproved" And cmbWebStatus = "Disapproved" Then
Cancel = False
If cmbInternalStatus = "Functional Analysis" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
If cmbInternalStatus = "Fwd for Cmd Analysis" And cmbWebStatus = "Fwd for Cmd Analysis" Then
Cancel = False
If cmbInternalStatus = "Fwd for Decision" And cmbWebStatus = "Fwd for Decision" Then
Cancel = False
If cmbInternalStatus = "Fwd to SME" And cmbWebStatus = "Fwd to SME" Then
Cancel = False
If cmbInternalStatus = "Ineligible" And cmbWebStatus = "Ineligible" Then
Cancel = False
If cmbInternalStatus = "Pending Payment" And cmbWebStatus = "Approved" Then
Cancel = False
If cmbInternalStatus = "Staffing (TL Only)" And cmbWebStatus = "Approved" Then
Cancel = False
If cmbInternalStatus = "Team Lead Review" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
If cmbInternalStatus = "Tech Review" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
Else
MsgBox "Internal Status does not match Web Status, please correct.", , "Silly Analyst!"
Cancel = True
End If

End Sub

MalenaC's photo
Mon 11/10/08 05:04 AM
Are you OK this morning?noway noway noway noway noway noway

someguy1313's photo
Mon 11/10/08 05:06 AM
Is it morning? noway I have been up all night working on this dumb database...drinker drinker drinker drinker drinker drinker drinker drinker drinker drinker laugh

no photo
Mon 11/10/08 06:37 AM

I am new to VBA does anyone know how this SHOULD have been written... I know every IF needs an END IF and the word OR does not seem to work so how would I cover all the below... and writing an IF for each does not work.

Private Sub cmbInternalStatus_Exit(Cancel As Integer)

If cmbInternalStatus = "Approved" And cmbWebStatus = "Approved" Then
Cancel = False
ElseIf cmbInternalStatus = "Closed (Other)" And cmbWebStatus = "Closed (Other)" Then
Cancel = False
ElseIf cmbInternalStatus = "Director Review" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
ElseIf cmbInternalStatus = "Disapproved" And cmbWebStatus = "Disapproved" Then
Cancel = False
ElseIf cmbInternalStatus = "Functional Analysis" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
ElseIf cmbInternalStatus = "Fwd for Cmd Analysis" And cmbWebStatus = "Fwd for Cmd Analysis" Then
Cancel = False
ElseIf cmbInternalStatus = "Fwd for Decision" And cmbWebStatus = "Fwd for Decision" Then
Cancel = False
ElseIf cmbInternalStatus = "Fwd to SME" And cmbWebStatus = "Fwd to SME" Then
Cancel = False
ElseIf cmbInternalStatus = "Ineligible" And cmbWebStatus = "Ineligible" Then
Cancel = False
ElseIf cmbInternalStatus = "Pending Payment" And cmbWebStatus = "Approved" Then
Cancel = False
ElseIf cmbInternalStatus = "Staffing (TL Only)" And cmbWebStatus = "Approved" Then
Cancel = False
ElseIf cmbInternalStatus = "Team Lead Review" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
ElseIf cmbInternalStatus = "Tech Review" And cmbWebStatus = "Functional Analysis" Then
Cancel = False
Else
MsgBox "Internal Status does not match Web Status, please correct.", , "Silly Analyst!"
Cancel = True
End If

End Sub


I think you want to use an ElseIf, if I'm understanding the question.

someguy1313's photo
Mon 11/10/08 07:05 AM
Edited by someguy1313 on Mon 11/10/08 07:31 AM
got it thanks

talldub's photo
Mon 11/10/08 05:30 PM
Or you could use CASE...

no photo
Tue 11/11/08 07:01 AM

Or you could use CASE...


He cannot use CASE, IF/Then logic is the way to go. There are AND clauses, which couldn't be included with a CASE.

AngieH79's photo
Tue 11/11/08 11:39 AM
My VBA is very rusty, but it looks like he's already using ElseIf.

talldub's photo
Tue 11/11/08 01:12 PM


Or you could use CASE...


He cannot use CASE, IF/Then logic is the way to go. There are AND clauses, which couldn't be included with a CASE.

Ever hear of nesting?

no photo
Tue 11/11/08 02:37 PM



Or you could use CASE...


He cannot use CASE, IF/Then logic is the way to go. There are AND clauses, which couldn't be included with a CASE.

Ever hear of nesting?


Yes, I have. Did you notice that he has 9 different AND clauses? ElseIf would be much more efficient and easier to read than a 12 Case statement with multiple sub IF/CASE statements.