basic IF statement problem.. i mean basic

antonyx

Arsenal Supporter
Local time
Today, 11:25
Joined
Jan 7, 2005
Messages
556
Code:
Private Sub idfound_KeyDown(KeyCode As Integer, Shift As Integer)
Dim idme
idme = idfound.Text
If KeyCode = 9 Then
If IsNull(idfound) Then
Me.personname.SetFocus Else
Forms!Job.qbook.Value = idme
DoCmd.Close
End If
End If
End Sub


basically if the user tabs from the field (keycode 9) and the field is empty.. focus on another field.. if it is not empty then send the value to another form and close the current form...

how should i use two if statements like this.
 
please.. would you be as kind as to show me..
 
Hi,
try it like this,

Code:
Private Sub idfound_KeyDown(KeyCode As Integer, Shift As Integer)
'if idfound is your control/textbox
If KeyCode = 9 Then
If IsNull(idfound) Then
Me.personname.SetFocus 
Else
Docmd.openform "YourFormName" 'is it Job?
Forms!Job!qbook.Value = idfound
'just in case you close the wrong form.
DoCmd.Close acform, "YourCurrentForm"
End If
End If
End Sub

basically if the user tabs from the field (keycode 9) and the field is empty.. focus on another field.. if it is not empty then send the value to another form and close the current form...

how should i use two if statements like this.
 
Assuming that the Job form is already open then something like this should work
Code:
If KeyCode = 9 And IsNull(Me.idfound) Then
    Me.personname.SetFocus
Else
    Forms!Job!qbook = Me.idfound
    DoCmd.Close
End If

but this will only fire if they tab out of the field, not if the hit enter or use the mouse

Peter
 
using this

Code:
Private Sub idfound_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 9 And IsNull(Me.idfound) Then
    Me.personname.SetFocus
Else
    Forms!Job!qbook = Me.idfound.Value
    DoCmd.Close
End If
End Sub

leaves the control as soon as you type something into it.. it doesnt give you a chance to type something and then press tab..and it doesnt send the value across either.. im workin on it.. but can you see why it dont work??
 
Last edited:
ok i got it to send the id across when you tab with this..

Code:
Private Sub idfound_LostFocus()
Dim newid
If KeyCode = 9 And IsNull(idfound) Then
    Me.personname.SetFocus
Else
    newid = idfound.Value
    Forms!Job!qbook.Value = newid
End If
End Sub

however when i tried to close the form here..

Code:
newid = idfound.Value
    Forms!Job!qbook.Value = newid
[b]here[/b]

after the id has been sent.. it tells me you cant close the form while the operation is being done or something..
 
Run-time error '2585':

This action can't be carried out while processing a form or report event.
 
it may be that you need to set KeyCode=0 to prevent the tab happening

Peter
 
ok i tried to focus on the job form to try and give the pax form time to close but i still get that problem.. can i add like a 3 second delay where the pax form closes???


Code:
Private Sub idfound_LostFocus()
Dim newid
If KeyCode = 9 And IsNull(idfound) Then
    Me.personname.SetFocus
Else
    newid = idfound.Value
    Forms!Job!qbook.Value = newid
    Forms("job").SetFocus
    DoCmd.Close acForm, "pax"
End If
End Sub
 
Code:
Private Sub idfound_LostFocus()
Dim newid
If KeyCode = 9 And IsNull(idfound) Then
    Me.personname.SetFocus
Else
    newid = idfound.Value
    Forms!Job!qbook.Value = newid
    Forms("job").SetFocus
End If
End Sub

there is another problem.. if the textbox is empty then it still focuses on the job form and sends a blank value to the job.qbook field..

when it is null it is supposed to goto the next field on the same form and completely ignore the other form..

why is this happening?
 
A couple of points.
Keycode is meaningless outside of the KeyDown type events. If you check its value it will be null as it is not set.
If you set Option Explicit as the first line at the top of your module it will spot variables that have not been declared.

Do you really only want to use the tab to trigger the event? the way it is at the moment if the field is blank and someone hits enter then it will still go to the new form.

You may need to check your field for an empty string as well as null
If Nz(Me.idfound, "") = "" Then

HTH

Peter
 
yes.. this is how i want it to be..

the idfound is a textbox i added to my pax (pax=passenger) form and will be used by my user typin in a number.. eg.. 1 or 12 or 345.. or 1203.. always numbers.. no letters

they reach the idfound textbox.. if they tab str8 past it without entering any value at all.. the focus can simply move to the next control.. which happens to be personname

if they type in a number.. when they press tab and there is a number in the control.. i want that number to pass to the qbook textbox on my job form.. (which will always be open if the pax form is open) and then just close the pax form..

is this clearer.. i realise i mite have to do another kind of null check.. but i dont think i can get it perfect unless you show me a snippet to try because im going backwards and forwards here at the moment
 
you have too much faith in your users :-)

Use the after update event of the idfound text box to jump forms. You can then validate what they have put in, make sure it is just a number and that it is in a valid range, before commiting to anything.

I will try to come up with some code in the morning as things are getting a bit tight here at the moment

Peter
 
yes ok.. i will try what i have got so far in the after update event and see what happens...

if anyone else can suggest a method i would be very grateful cos my boss is bustin my balls...

thank you
 
i think i fixed it.. i just used the after update and used this code and it transfers the value if something is in it and it moves to the next field if its empty..

thank you batman.. biggup the uk

Code:
Private Sub idfound_AfterUpdate()
Dim newid
If KeyCode = 9 And IsNull(idfound) Then
    Me.personname.SetFocus
Else
    newid = idfound.Value
    Forms!Job!qbook.Value = newid
    Forms("job").SetFocus
    DoCmd.Close acForm, "pax"
End If
End Sub
 
if they miskey a value you still get it. You should always try to test data.

Code:
Private Sub idfound_AfterUpdate()
If Not IsNumeric(Me.idfound) Then
    MsgBox "Invalid Data", vbCritical, "Warning"
Else
    Forms!Job!qbook = Me.idfound
    DoCmd.Close
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom