Form_Load code not running

  • Thread starter Thread starter Jonathan Stratford
  • Start date Start date
J

Jonathan Stratford

Hi,

I use docmd.openform to open a form, but when it opens,
the form_load code does not run. What am i doing wrong,
and what is the easiest way to fix this issue?

Many thanks,

Jonathan Stratford
 
Form_Load may not fire if the form is already open in some view at the time.

If that is not the case, temporarily comment out any error handling from the
event, and add this line to the top of the routine:
Debug.Print "Form_Load for " & Me.Name & " at " & Now()
My guess is that it is firing.
 
Hi,

It appears that it is firing, but for some reason nothing
gets done!!!! Here's my code, can anyone please point out
the problems. NB The msgbox "hi" in the first function
never appears. I am logged in as "Admin", and i'm coding
it this way because i don't have time to do the security
the proper way - it caused me problems! Also, it wouldn't
help me for this i don't think.

Many thanks,

Jonathan Stratford


Private Function findUserType() As String
Dim i As Integer
Call getUserArrays 'use call to ensure it's finished
before code continues
MsgBox "hi"
For i = 1 To 100

If CurrentUser = theLineManagers(i) Then
findUserType = "Line Manager"
Exit Function
ElseIf CurrentUser = theAdmins(i) Then
findUserType = "Admin"
Exit Function
Else 'if not admin or line manager must just be user
findUserType = "User"
Exit Function
End If
Next
End Function

Private Sub getUserArrays()
theUsers(1) = "User1"
theUsers(2) = "User2"
theLineManagers(1) = "Jonny"
theAdmins(1) = "Admin"
End Sub

Private Sub Form_Load()
Debug.Print "Form_Load for " & Me.Name & " at " & Now()
'use CurrentUser to determine whether a member of admin,
line managers or users
Dim usertype As String
usertype = findUserType
If usertype = "Admin" Then
cmdAddEmployee.Visible = True
cmdAddGroup.Visible = True
End If
If usertype = "Line Manager" Then
cmdAddEmployee.Visible = False
cmdAddGroup.Visible = False
End If
End Sub
 
You may be erroring out before it gets to the message box.
Try this: Comment out the debug statement provided by
Allen. Put in the following: On Error Goto 0.

Also put this into the getUserArrays function.

My guess is there is an error being thrown in the
getUserArrays function that is stopping the code
execution. The On Error goto 0 (that's zero) command will
throw an error message that will provide some information.
Also I suggest you put a breakpoint in at the Call
statement for the getUserArrays function. When you hit
that breakpoint, use the F8 key to step through the code
one step at a time. That will get you started. Post back
with what you find and we can go from there. If you get an
error message, make note of both the error number and the
associated information. Search help, then if more
information is needed, search the knowledge base.

Lastly, if the error you encounter is something you might
expect in production (say a field value being null or
blank (empty string) then you need to provide an error
handling routine. Post back if you need more help there.

Hope that helps!
 
Hi,

My code is now:

Private Function findUserType() As String
Dim i As Integer
Call getUserArrays 'use call to ensure it's finished
before code continues
MsgBox "hi"
For i = 1 To 100

If CurrentUser = theLineManagers(i) Then
findUserType = "Line Manager"
Exit Function
ElseIf CurrentUser = theAdmins(i) Then
findUserType = "Admin"
Exit Function
Else 'if not admin or line manager must just be user
findUserType = "User"
Exit Function
End If
Next
End Function

Private Sub getUserArrays()
On Error GoTo 0
MsgBox "hello"
theUsers(1) = "User1"
theUsers(2) = "User2"
theLineManagers(1) = "Jonny"
theAdmins(1) = "Admin"
End Sub

Private Sub Form_Load()
On Error GoTo 0
'Debug.Print "Form_Load for " & Me.Name & " at " & Now()
'use CurrentUser to determine whether a member of admin,
line managers or users
Dim usertype As String
usertype = findUserType()
If usertype = "Admin" Then
cmdAddEmployee.Visible = True
cmdAddGroup.Visible = True
End If
If usertype = "Line Manager" Then
cmdAddEmployee.Visible = False
cmdAddGroup.Visible = False
End If
End Sub

I think I've followed your instructions. However, no
error comes up at all, and i don't get the chance to step
through the code, so it obviously never gets to that
point. It seems this is going to be much harder than I'd
anticipated! I added the msgbox "hello" and that doesn't
happen either! Hmmmm. Any suggestions much appreciated.
Is it likely to be to do with security settings which I
tried unsuccessfully to apply?

Thanks,

Jonathan Stratford
 
Back
Top