basic else if problem

antonyx

Arsenal Supporter
Local time
Today, 12:27
Joined
Jan 7, 2005
Messages
556
trying to turn this..

Code:
Private Sub idfound_AfterUpdate()
Dim newid
If KeyCode = 9 And IsNull(idfound) Then
    Me.cboname.SetFocus
Else
    newid = idfound.Value
    If CurrentProject.AllForms("job_cash_single").IsLoaded Then
    Forms!job_cash_single!qpax.Value = newid
    DoCmd.Close acForm, "job_add_passenger"
    Else
    Forms!job_card_single!qpax.Value = newid
    DoCmd.Close acForm, "job_add_passenger"
End If
End If
End Sub

into this and its not correct.. how shall i lay it out..

Code:
Private Sub idfound_AfterUpdate()
Dim newid
If KeyCode = 9 And IsNull(idfound) Then
    Me.cboname.SetFocus
Else
    newid = idfound.Value
    If CurrentProject.AllForms("job_cash_single").IsLoaded Then
    Forms!job_cash_single!qpax.Value = newid
    DoCmd.Close acForm, "job_add_passenger"
    Else[b]If[/b]
    Forms!job_card_single!qpax.Value = newid
    DoCmd.Close acForm, "job_add_passenger"
    [b]ElseIf
    Forms!job_card_daily!qpax.Value = newid
    DoCmd.Close acForm, "job_add_passenger"[/b]
End If
End Sub
 
2 thoughts. First, look in Help for the proper structure of the ElseIf. It's basically like the If line. Second, I'm curious what value you're getting for KeyCode, as that's typically used in Key events (Key Press, etc), not update events.
 
i know its quite dumb..

i have unconventional methods..

elseif cant be like if because it doesnt use then.. does it?
 
Have you looked in Help? I mean, you can look there in less time than it takes to post here.
 
i have looked yeah.. i typed

if.. and elseif.. and it takes me online to search.. i did offline search briefly.. cudnt really see anything..


none of the results show an elseif example
 
From Help:

Code:
If condition Then
  [statements]
[ElseIf condition-n Then
  [elseifstatements] ...
[Else
  [elsestatements]]
End If
 
Hi antonyx,

You really need to have the action/events planned first before jumping into coding, not by hammering and screwing into your codes.

Try this,

Code:
Private Sub idfound_AfterUpdate()
Dim newid
newid = idfound.Value

If KeyCode = 9 And IsNull(idfound) Then
    Me.cboname.SetFocus
    Exit Sub ‘exit event since it’s doing nothing other than setfocus
End If
‘not going to check code below since there is no plan of action
‘and assuming forms below were open and running.    
If CurrentProject.AllForms("job_cash_single").IsLoaded Then
Forms!job_cash_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"
    	ElseIf
   	Forms!job_card_single!qpax.Value = newid
    	DoCmd.Close acForm, "job_add_passenger"
    		ElseIf
   	 	Forms!job_card_daily!qpax.Value = newid
    		DoCmd.Close acForm, "job_add_passenger"
End If
End Sub
 
ElseIf....what??

That code won't even compile, no less run.
 
unclejoe.. that code doesnt work..

im trying something like this..

still not working but maybe someone can tell me if im on the right track..

Code:
Private Sub idfound_AfterUpdate()
Dim newid
newid = idfound.Value

If KeyCode = 9 And IsNull(idfound) Then
    Me.cboname.SetFocus
    Exit Sub
    
If CurrentProject.AllForms("job_cash_single").IsLoaded Then
Forms!job_cash_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

ElseIf CurrentProject.AllForms("job_card_single").IsLoaded Then
Forms!job_card_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

Else
Forms!job_card_daily!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"
End If

End Sub
 
First of all, this part
If KeyCode = 9
is useless in a before update event. The only time it's good is in a KeyDown, KeyPress, or KeyUp event. Now, if you are using the reserved word KeyCode for something like a variable, it won't work because KeyCode is an Access reserved word.

2nd, you're missing an extra End If in there for that first part(which won't work as written anyway
If KeyCode = 9 And IsNull(idfound) Then
Me.cboname.SetFocus
Exit Sub

3rd, IsLoaded is not an existing Access function. There is code available on this site, and on other sites, for that function but it isn't native so to use it you would need to either write the function or create a module and copy existing code in.
 
I agree with BL on points 1 & 2 (I mentioned the KeyCode thing in my first post). I'll have to disagree with 3 though. IsLoaded is not a native function, but it is a native property, and I suspect will work as written here.
 
I guess I didn't do it properly as it did work on my second try after seeing Paul's post. My bad.
 
As Paul and Bob have said about “KeyCode” thing, it appears that you need to put a line “Option Explicit” just below “Option Compare Database”

When you compile and run the form or code, the editor will inform you that “Keycode” – Variable Not Define.

Since “KeyCode” is reserved name in Access, you can’t use it and it’s useless in that AfterUpdate event. (I gave you the benefit of doubt since “Keycode” might be a control on your form and of course you can’t name it “Keycode”).

Which part of the code/line doesn’t work?

Try this,

Code:
Private Sub idfound_AfterUpdate()
Dim newid
newid = idfound.Value

If IsNull(idfound) Then  ‘since “KeyCode” is useless remove it
    Me.cboname.SetFocus
    Exit Sub
End if  ‘you need end if here not below 

If CurrentProject.AllForms("job_cash_single").IsLoaded Then
Forms!job_cash_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

ElseIf CurrentProject.AllForms("job_card_single").IsLoaded Then
Forms!job_card_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

Else
Forms!job_card_daily!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"
End If

End Sub

If you want to use the “KeyCode”, put this into the idfound KeyDown event.

Code:
Private Sub idfound_KeyDown (KeyCode As Integer, Shift As Integer)
If KeyCode = 9 then ‘if user press the “Tab” key
Me.cboname.SetFocus
End If
End Sub

Note: do not use breakpoints (your VB editor) in the keydown event, it prevents you from changing the value of the control and the afterupdate will not fire.

What if the user edits and press “Enter” key? Well, you can add,

“If KeyCode = 9 or KeyCode = 13 then”
or
Select Case KeyCode
Case 9 or 13
Me.cboname.SetFocus
End Select

unclejoe.. that code doesnt work..
 
unclejoe said:
Which part of the code/line doesn’t work?

Not sure what you're asking, but the code you posted earlier includes

ElseIf

on lines by itself, which is invalid syntax and won't compile or run.
 
Also I think that this would all be a lot simpler if "job_add_passenger" was opened as a popup modal form, and made invisible instead of being closed.
The code opening the form will stop running until "job_add_passenger" loses control then the wanted value can be taken from the invisble form and the form closed.
 
The code I post will not work. But look at post #9. Look at the If ElseIf, it should work except the missing End If.

As there aren’t any error messages, I won’t conclude anything, as the ElseIf was incomplete in the beginning.

Usually, a beginner doesn’t understand what Compiling code means. There are cases when codes keyed into a Form’s module runs without error messages especially without the “Option Explicit”.

If there were an error, than, If Else line would show up, like this one.

“If KeyCode = 9 And IsNull(idfound) Then”

Thus, “Variable Not Define” error would show up. The “KeyCode” was undefined in that event and will be highlighted in dark blue.

Than, the “KeyCode” question will arise. As usual, beginners are clueless when it comes to naming conventions in Access.

pbaldy said:
Not sure what you're asking, but the code you posted earlier includes

ElseIf

on lines by itself, which is invalid syntax and won't compile or run.
 
so are you telling me that this code will work??

Code:
Private Sub idfound_AfterUpdate()
Dim newid
newid = idfound.Value

If KeyCode = 9 And IsNull(idfound) Then
    Me.cboname.SetFocus
    Exit Sub
    
If CurrentProject.AllForms("job_cash_single").IsLoaded Then
Forms!job_cash_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

ElseIf CurrentProject.AllForms("job_card_single").IsLoaded Then
Forms!job_card_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

Else
Forms!job_card_daily!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"
End If
End If
End Sub
 
Hi antonyx,

You need to run and test the code. I wouldn’t say the code would work. But I will ask you; does it work according to what you have designed it to do?

Don’t ask me because I can’t see your database and how you develop it and you didn’t even test it.

Did you read post #16?

Did you read the code comments in post #13?

Did you test the Code? Was there any error?

Did you put “Option Explicit” into the Form’s module?

Did you read previous post by others about “KeyCode”.

Does “KeyCode” exist in your current form as a Control or is it a variant defined somewhere? My guess is not likely to be. Besides, you can’t name something that is reserved in Access and it will cost more headaches later.

The event will fire, but since “If KeyCode = 9 and isNull(idfound) Then” is False, it will skip the rest of the code. Hence, the code works but not accord to your intentions or planned action.

Please read post #16 again about the part on “KeyCode” error.

Let’s look at this part of the code again in post #13.

>If IsNull(idfound) Then ‘since “KeyCode” is useless remove it
> Me.cboname.SetFocus
> Exit Sub ‘exit event because idfound is Null, return the form to user
>End if ‘you need end if here not below – (not after the next If ElseIF)

If the If statement is True, it means that the Control “idfound” is Null.
The next line of code will setfocus to Control “cboname” of your current form and exit event and return the current form to the user.

Just below “Me.cboname.SetFocus” line, the “Exit Sub” means exit the event now! And will skip the rest of the code. The rest depends on what you want to do next. Most likely, you miss the "Else"

If IsNull(idfound) Then
Me.cboname.SetFocus
Exit Sub
Else ''i think you miss something here
'do or check something here or part of the code here
End If

I hope you have understood this part of the code.
Good Luck.

antonyx said:
so are you telling me that this code will work??

Code:
Private Sub idfound_AfterUpdate()
Dim newid
newid = idfound.Value

If KeyCode = 9 And IsNull(idfound) Then [B]KeyCode still here? if True exit event? Do nothing?[/B]
    Me.cboname.SetFocus
    Exit Sub
    [B]Why no Else here? What your plan? If False exit event here? do nothing?[/B]
If CurrentProject.AllForms("job_cash_single").IsLoaded Then
Forms!job_cash_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

ElseIf CurrentProject.AllForms("job_card_single").IsLoaded Then
Forms!job_card_single!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"

Else
Forms!job_card_daily!qpax.Value = newid
DoCmd.Close acForm, "job_add_passenger"
End If
End If
End Sub
 
yeah i have the habit of asking before trying.. plus i was only working on it for an hour or two today so i tried gettin a quicker answer.. i wont continue this habit
 

Users who are viewing this thread

Back
Top Bottom