Before Form Loads...(OnLoad)

DanG

Registered User.
Local time
Today, 13:22
Joined
Nov 4, 2004
Messages
477
As my main form loads (onload), I would like it to check for users (have that solved) and if thee user is #1 level keep loading the current form (frmMSEntry). If the user lever is anything else but #1, I want it to load another form (frmReportCentral) using VBA.

I have it for the most part and have isolated the problem to frmMSEntry. When this form closes I get an "Enter Perameter Value" for "Forms!frmMSREntry!ServiceRequested" which is a field on frmMSREntry. During normal day to day use I have no problem whith this field, only in trying to do what I am doing here is it a problem. But there is a lot going on with this field behind the scenes (VBA). I think in the end if I were to guess I'd day it was an event problem, because I really want to run the test (VBA) before anythings starts to load?

Hopefully this is enough info?

I appreciate any suggestions!

Thank you

frmMSREntry Code (just coded for testing, not full featured yet):
Code:
Private Sub Form_Open(Cancel As Integer)
 If CurrentProject.AllForms("frmCurrentUserHidden").IsLoaded Then
    Dim level As String
    level = Nz(Forms.frmCurrentUserHidden!UserLevel)
   Select Case level
    Case "1"
        DoCmd.Close acForm, "frmMSREntry"

    Case "4"
        MsgBox ("You are not #1")
    Case Else
        MsgBox "You Are Not Authorized To Enter This Area", vbCritical
 
    End Select

End If

End Sub

ServiceRequested field VBA for the "AfterUpdate" event:
Code:
Select Case Me.ServiceRequested.Value
    Case 3, 11
        Me.ServiceType.Enabled = True
        Me.Campaign.Enabled = False
        Me.Campaign.Value = Null
        Me.CampReturnScreen.Visible = False
    Case 2
        Me.ServiceType.Enabled = True
        Me.CampReturnScreen.Visible = False
        Me.Campaign.Enabled = False
        Me.Campaign.Value = Null
    Case 10
        Me.Campaign.Enabled = True
        Me.ServiceType.Enabled = False
        Me.ServiceType.Value = Null
        Me.CampReturnScreen.Visible = True
    Case Else
        Me.ServiceType.Enabled = False
        Me.Campaign.Enabled = False
        Me.ServiceType.Value = Null
        Me.Campaign.Value = Null
        Me.CampReturnScreen.Visible = False
End Select
        
If Me.ServiceRequested <> 10 Then
    DoCmd.SetWarnings False
    DoCmd.OpenQuery ("qryPurgeCampaignReturned")
    Me.CampReturnScreen.Requery
    DoCmd.SetWarnings True

End If
 
Is it feasible to do this test from the form that opens this one, so it opens one or the other as appropriate? That's what I would do.
 
There is no form that opens this one.
This form loads as the startup form (through autoexec actually).
 
I figured it opened from frmCurrentUserHidden. You can perform a similar test in your autoexec macro, and open the appropriate form.
 
I figured it opened from frmCurrentUserHidden. You can perform a similar test in your autoexec macro, and open the appropriate form.

That is a hidden form that contains the users profile so I can direct them to where I want them to go.

But might be able to use that, I'll give it a go.
 
I think all you are missing is a CANCEL or two

take this - your original code


Code:
Private Sub Form_Open(Cancel As Integer)
 If CurrentProject.AllForms("frmCurrentUserHidden").IsLoaded Then
    Dim level As String
    level = Nz(Forms.frmCurrentUserHidden!UserLevel)
    Select Case level
      Case "1"

[COLOR="Red"]
'assuming your current form is "frmmsentry" then don't do this[/COLOR]        
        [B]DoCmd.Close acForm, "frmMSREntry"[/B]

[COLOR="red"]'instead do this (nothing else is needed - 
cancel = true tells the form not to open
exit sub exits the sub, and lets the form close
[/COLOR]
[B][COLOR="Blue"]       
        cancel = true
        exit sub[/COLOR][/B]


    Case "4"
        MsgBox ("You are not #1")
        MsgBox "You Are Not Authorized To Enter This Area", vbCritical
[COLOR="blue"]                
        cancel = true
        exit sub
 [/COLOR]   End Select

End If

End Sub
 
I think all you are missing is a CANCEL or two

take this - your original code


Code:
Private Sub Form_Open(Cancel As Integer)
 If CurrentProject.AllForms("frmCurrentUserHidden").IsLoaded Then
    Dim level As String
    level = Nz(Forms.frmCurrentUserHidden!UserLevel)
    Select Case level
      Case "1"

[COLOR="Red"]
'assuming your current form is "frmmsentry" then don't do this[/COLOR]        
        [B]DoCmd.Close acForm, "frmMSREntry"[/B]

[COLOR="red"]'instead do this (nothing else is needed - 
cancel = true tells the form not to open
exit sub exits the sub, and lets the form close
[/COLOR]
[B][COLOR="Blue"]       
        cancel = true
        exit sub[/COLOR][/B]


    Case "4"
        MsgBox ("You are not #1")
        MsgBox "You Are Not Authorized To Enter This Area", vbCritical
[COLOR="blue"]                
        cancel = true
        exit sub
 [/COLOR]   End Select

End If

End Sub


I got it working by using "OnLoad" on the other form which in the end may be the better way to do it???

But the cancel is originally what I was looking for (and will put away in my toolbox :)), I just didn't know how to go about it.

Thanks guys!!!!!
 
if you see a cancel in ANY event header

(it applies to things like
- field beforeupdate
- form beforeupdate
- load
etc)

and you change your mind, you can use the cancel to, well, cancel the update,

so you can use this for validation - eg ask if the price is correct, and if not cancel the action - see idea below

Code:
myprice_beforeupdate(cancel as integer)
if myprice>20 then
  if msgbox("Are you sure the price is over 20",vbyesno) = vbno then
[COLOR="Blue"]    cancel = true
    exit sub[/COLOR]  
  end if
end if
end sub
 
Dave,

After re-reading your post and digesting it completely, I now get the bigger picture...thank you, thank you.

I really don't give error handling too much consideration, but this is something to consider and implement.
 

Users who are viewing this thread

Back
Top Bottom