If Statement on a list box?

New Contributor

Hello, Im hoping I can get some help with a statement I want to put on a list box. I am making a Key-checkout form. I have a listbox of key numbers. Employees can have more than one key assigned to them, so I made separate Text boxes for each key number. So first key- 2nd key-3rd key and so forth. What I want to do is have that one listbox check if the first key box is empty. If it is, the key number goes there, if not, then it goes to text box 2, and so forth. The code I have is below, but it puts out a syntax error. I dont know what is wrong, as its been years since I've even looked at access. I'm not sure if I'm putting it in the wrong place (Under event procedure for the listbox)? Any help will be greatly appreciated :)

 

=iF IsNull(Key1) Then Key1 = ListBox ElseIf IsNull(text86) Then text86 = ListBox ElseIf IsNull(text103) Then text103 = ListBox ElseIf IsNull(text105) Then text105 End If

This brings back a invalid Syntax error

4 Replies

@Data2link Your syntax looks OK, provided you have the correct spacing and that's not all just on one line:

 

If X=1 Then

   doThis

Elseif X=2 Then

   doThis

Else

   doThat

End If

 

Also, don't put an = sign in front of that. See these free videos for more help: 

 

Intro to VBA

AfterUpdate

 

Richard Rost

MVP 2014-2015

Access Learning Zone

 

@RichardRost 

 

Ok thank you so much. I will try that. I should be putting this under the listbox event correct?

The list box's AfterUpdate event, yes. And I'd rename the list box to something like KeyList, and name those text boxes something better as well. So you should end up with something like:

If IsNull(Key1) Then
Key1 = KeyList
Elseif IsNull(Key2) Then
Key2 = KeyList
Else
Key3 = KeyList
End If
OK thank you- I was putting it in the wrong place. Gonna give this a shot. (Great videos btw)