Refer to another forms control

S

SoggyCashew

Hello, I have a form frmCalendar with 2 check boxs chkEmployee and
chkDepartment and a OK button. You have to either check one of these boxes
before proceding and click the button that opens a form frmPasswordRequired
and sets the frmCalendar to visible = False. After entering a password and
clicking OK it would open one of two forms due to whatever box you checked on
the frmCalendar. I cant get it to open either of the two forms. Here is what
im using:


If Forms!frmCalendar!chkEmployee = True Then
DoCmd.OpenForm "frmEmployeeStatusChange"
DoCmd.Close acForm, "frmPasswordRequired"

If Forms!frmCalendar!chkDepartment = True Then
DoCmd.OpenForm "frmDepartment"
DoCmd.Close acForm, "frmPasswordRequired"
 
R

Ryan

Try this.
If Forms![frmCalendar]![chkEmployee] = True Then
DoCmd.OpenForm "frmEmployeeStatusChange"
DoCmd.Close acForm, "frmPasswordRequired"
End If
End Sub

If Forms![frmCalendar]![chkDepartment] = True Then
DoCmd.OpenForm "frmDepartment"
DoCmd.Close acForm, "frmPasswordRequired"
End If
End Sub
 
S

SoggyCashew

Yes it is part of an option group! How could I get this to work? Here is the
complete code:

Private Sub cmdLogin_Click()
'Declare a Static Integer variable to hold the
'number of times a password was entered. In this
'code, only 3 tries are permiteed. A Static Variable
'will always retain the value of a Variable for as
'long as the Form is open. It is very much the same as
'as Form global variable except that it can only be
'used within the Procedure that declared it. In other
'words, it is global to the Procedure only.
Static Cnt As Integer

'Trap Errors
On Error GoTo Error_cmdLogin

'Ensure focus on the TextBox
Me.txtPassword.SetFocus

'Read the text within the TextBox and see if it contains
'our desired Password...If it does not then...
If Me.txtPassword.Text <> "soggycashew" Then
'No it doesn't so...
'Increment out Password entry counter
Cnt = Cnt + 1
'If the Counter (Cnt) = 3 then the Password was
'unsuccessfuly entered three times...
If Cnt = 3 Then
'Inform the User he/she has failed.
MsgBox "You have tried three times and failed to Login." & vbCr & _
"See your Database Administrator for assistance.",
vbExclamation, _
"Password Entry Failure"
'Zero the Counter
Cnt = 0
'Close the Password entry Form
DoCmd.Close acForm, "frmPasswordRequired"
Forms!frmCalendar.Visible = True

'Get outta this procedure
Exit Sub
End If
'The Password ntry Counter (Cnt) is still not equal to 3
'So inform the User that He/She has entered the wrong
'password and inform of how many seconds are remaining to
'enter the correct Password.
MsgBox "Incorrect Password. Please Try Again." & vbCr & _
"You Have " & (60 - TimeCount) - 1 & " Seconds Remaining.", _
vbInformation, "Password Incorrect"
'Clear the TextBox of whatever was entered previously.
Me.txtPassword = ""
'Get outta this procedure.
Exit Sub
Else

If Forms!frmCalendar!chkEmployee = True Then
DoCmd.OpenForm "frmEmployeeStatusChange"
DoCmd.Close acForm, "frmPasswordRequired"

End If

If Forms!frmCalendar!chkDepartment = True Then
DoCmd.OpenForm "frmDepartment"
DoCmd.Close acForm, "frmPasswordRequired"

End If
Exit Sub

Error_cmdLogin:
Err.Clear
End If
End Sub
 
D

Dirk Goldgar

SoggyCashew said:
Yes it is part of an option group! How could I get this to work? Here is
the
complete code:
[...]
'Ensure focus on the TextBox
Me.txtPassword.SetFocus

'Read the text within the TextBox and see if it contains
'our desired Password...If it does not then...
If Me.txtPassword.Text <> "soggycashew" Then

You wouldn't normally have to SetFocus to the text box and then read its
Text property. Normally, one would just read the Value property (its
default property) and not have to set the focus there to do it:

If Me.txtPassword <> "soggycashew" Then

P.S. I hope that's not your real password, or you just have it away to the
whole world. said:
[...]
Else

If Forms!frmCalendar!chkEmployee = True Then
DoCmd.OpenForm "frmEmployeeStatusChange"
DoCmd.Close acForm, "frmPasswordRequired"

End If

If Forms!frmCalendar!chkDepartment = True Then
DoCmd.OpenForm "frmDepartment"
DoCmd.Close acForm, "frmPasswordRequired"

End If
Exit Sub

Error_cmdLogin:
Err.Clear
End If
End Sub

From the looks of that leading Else, you're still short one End If. Did
this code compile without errors?

However, those comments don't address your main question, which is how to do
this if the check boxes are in an option group. You didn't give the name of
the option group, so I'll use "optEmpOrDept" in my example. It's a little
simpler to do this in a Select Case structure, so here's my suggested
revision:

With Forms!frmCalendar

Select Case !optEmpOrDept

Case !chkEmployee.OptionValue
DoCmd.OpenForm "frmEmployeeStatusChange"
DoCmd.Close acForm, "frmPasswordRequired"

Case !chkDepartment .OptionValue
DoCmd.OpenForm "frmDepartment"
DoCmd.Close acForm, "frmPasswordRequired"

Case Else
' Anything to do here?

End Select

End With


Note: you could also hard-code the option values of the check boxes, rather
than pulling them from the OptionValue properties of the controls
themselves, but I find doing it the latter way more flexible.
 
W

Wayne-I-M

You could try this (much smaller/simpler code)

Static Counter As Integer
If txtPassword = "soggycashew" Then
DoCmd.OpenForm "SomeOtherForm", acNormal, "", "", , acNormal
DoCmd.Close acForm, "frmPasswordRequired"
Else
If Counter < 2 Then
MsgBox "The password you entered is not correct - try again - MAX 3
ATTEMPTS?", vbOKOnly, "Database entry declined"
Counter = Counter + 1
txtPassword = ""
Else
DoCmd.Quit
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top