Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lock Subform if main form is empty

Status
Not open for further replies.
Nov 6, 2002
89
CH
Hello there

I have a mainform with a subform.

I would like to prevent users from entering data into the subform before they enter data into the mainform (mainform fields must be completed first!).

Let's say the main form is called "FormA" and the subform is called "SubFormB".

How can I achieve that?

So I have tried it with the following code but it is not working.

_________

Dim i as Integer
Dim blnLock as Boolean
'
blnLock = False
For i = 0 to Me.Controls.Count -1
Select case Me.Controls(i).ControlType
Case acLabel, acSubForm, acCommandButton
' do Nothing
Case Else
If IsNull(Me.Controls(i).Value) Then
blnLock = True
i = Me.Controls.Count
end if
End Select
Next i
Me.SubFormControlName.Locked = blnLock

______________

Any help is very much appreciated.


Thanks very much.

Stefan

 
First in your main form load event set the subform.enabled = False. Then run some validation code on your main form that runs on your main form's current event to find out if all your fields are entered correctly. If so then you set the subform.enabled = True.
 
'On second thought do it on the after update event of the main form

Private Sub FormA_Open(Cancel As Integer)
SubFormB.Enabled = False
End Sub


Private Sub FormA_AfterUpdate()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
If IsNull(ctl) = False Then
SubFormB.Enabled = True
End If
End If
Next ctl
End Sub

 
Thanks Omega36 for this code.

I think this is the right way to get to the solve this problem. However, the subform seems now to be locked even though the mainform fields are all field out. (At least, there is no error message coming up.)

Does anyone now why?

Thanks. Stefan
 
Hi Omega36

May I ask you to give me your first opinion as soon as possible since this database should be working soon.

Thank you .

Stefan


 
Ok, my mistake, I forgot something. You need to do it a little differently. Create a function in the formA like so:

Public Function LockSubForm()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
If IsNull(ctl) Or ctl = "" Then
subformB.Enabled = False
Exit For

Else
SubformB.Enabled = True

End If
End If
Next ctl
End Function

Now call this function when you open the form AND on the after update events of each control on your main form. Only way I could get it to work.

FormA_Open()
Call LockSubForm
End Sub

Control_AfterUpdate()
Call LockSubForm
End
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top