Hide A Form based on an If statement

Paul Cooke

Registered User.
Local time
Today, 19:19
Joined
Oct 12, 2001
Messages
288
Could someone please advise me how to hide a form if a text control on it is null?

At the moment I have the following code

Code:
Private Sub Form_Current()
'Looks up current Patient Name and DOB to show on forms


Dim strFirstName As String
Dim strSurname As String
Dim strDob As String

    strFirstName = Forms!FindPatientVaccination.lstPatients.Column(1)
    strSurname = Forms!FindPatientVaccination.lstPatients.Column(2)
    strDob = Forms!FindPatientVaccination.lstPatients.Column(3)
    Me.txtPatientName = strFirstName & " " & strSurname & " - " & strDob

'Looks up patient record and populates relevant controls on vaccination form

If Me.OpenArgs = "Vaccination" Then

    Me.txtPatientID = Forms!FindPatientVaccination.lstPatients
    lngPatientID = Forms!FindPatientVaccination.lstPatients
    Me.txtPatientFirstName = DLookup("PatientFirstName", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.txtPatientSurname = DLookup("PatientSurname", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.txtHomeAddressLine1 = DLookup("HomeAddressLine1", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.txtHomeAddressLine2 = DLookup("HomeAddressLine2", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.cboUkCityTownVillageID = DLookup("UkCityTownVillageID", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.cboUKCountyID = DLookup("UkCountyID", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.txtHomeAddressPostCode = DLookup("HomeAddressPostCode", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.txtEmailAddress = DLookup("EmailAddress", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.txtHomeTelephoneNumber = DLookup("HomeTelephoneNumber", "PatientDetails", "PatientID= " & lngPatientID & "")
    Me.txtTelephoneNumberMobile = DLookup("TelephoneNumberMobile", "PatientDetails", "PatientID= " & lngPatientID & "")
    
    
End If

'Checks to see if a new record has been selected and looks up new number

If Me.NewRecord = True Then

    lngTrNum = DLookup("NextVaccinationNumber", "ReferenceNumbers")
    strTrPrefix = DLookup("VaccinationPrefix", "ReferenceNumbers")
    Me.txtVaccinationNumber = strTrPrefix & lngTrNum

End If

If IsNull(Me.txtPatientID) Then

    Me.txtPatientID = Forms!FindPatientVaccination.lstPatients

End If

'check to see if patient details are complete

If IsNull(Me.txtEmailAddress) = True Then

    Beep
    MsgResponse = MsgBox("There is no email address on record for this patient, please update the missing information before entering the vaccination details." & vbCrLf & vbCrLf _
    & "It is important to ensure we have an email address so we can inform the patient when thier vaccines are due for a booster or are about to expire." & vbCrLf & vbCrLf _
    & "Does the patient have an email address?", vbYesNo, "Information Needed")

If MsgResponse = vbYes Then

    DoCmd.OpenForm "FindPatientAmend", , , , , , "VaccinationAmend"

    MsgBox "Please select the correct patient and complete the missing information in the Fields highlighted in red.", vbInformation, "Information"

    End If
End If

End Sub

At the moment the Vaccination form opens 'on top' but I want this form to be hidden if the Email address field is empty. I have tried loads of different bits of code after :-

Code:
If IsNull(Me.txtEmailAddress) = True Then

but nothing seems to work.

I am not sure if this would be better off in the Onload event but am resisting doing that as it would mess up the flow of the message boxes

Is there anyway to resolve this in the Oncurrent event?

many thanks

Paul
 
The code which you want is:
Code:
'Form:
FormName.visible = false
'Subform:
MainFormName!SubFormControlName.Visible = false

Current vs. on load depends on whether you want the code run each time the record it is viewing changes or each time the form loads (if the form is only ever displaying 1 record and cannot change record without closing the form then these are the same thing).
 
Thanks so much I can't believe I was so stupid in forgetting about the Subform !! it all makes sense now !!

thanks again
 
the other thing is - testing for null would not pick up a blank string (zero length string) - so instead of

If IsNull(txtEmailAddress) Then (you don't need the = true, or the me qualifier)

you could instead say

if len(nz(txtemailaddress,""))=0 then
 
oops maybe spoke to soon !

its hiding the subform but not the mainform - the subform has the focus on opening so not sure why this is not working ?

any ideas?

thanks
 
Thanks Dave - I wasn't aware of that but it does make sense So i will change it now !

thanks again
 
What is the exact code you are using now (just the block of code relating to checking the control for null, & hiding the form, etc. Not the unrelated stuff like the code at the start which populates controls)?
 
Im just having a quick play with it as the control on the main form that is getting the focus is a 'required' field so i'm think that may be blocking it from hiding

will post back in a mo
 
nope that didn't work ... so what I have at the moment is

Code:
If Len(Nz(txtEmailAddress, "")) = 0 Then
'If IsNull(Me.txtEmailAddress) = True Then

    Beep
    MsgResponse = MsgBox("There is no email address on record for this patient, please update the missing information before entering the vaccination details." & vbCrLf & vbCrLf _
    & "It is important to ensure we have an email address so we can inform the patient when thier vaccines are due for a booster or are about to expire." & vbCrLf & vbCrLf _
    & "Does the patient have an email address?", vbYesNo, "Information Needed")

If MsgResponse = vbYes Then

    Forms!VaccinationPrivate!VaccinationConsumablesSubform.Visible = False
    Forms!VaccinationPrivate.Visible = False
    DoCmd.OpenForm "FindPatientAmend", , , , , , "VaccinationAmend"
 
Try this:

Code:
If Len(Nz(txtEmailAddress, "")) = 0 Then
'If IsNull(Me.txtEmailAddress) = True Then
 
    Beep
    MsgResponse = MsgBox("There is no email address on record for this patient, please update the missing information before entering the vaccination details." & vbCrLf & vbCrLf _
    & "It is important to ensure we have an email address so we can inform the patient when thier vaccines are due for a booster or are about to expire." & vbCrLf & vbCrLf _
    & "Does the patient have an email address?", vbYesNo, "Information Needed")
 
If MsgResponse = vbYes Then
    DoCmd.OpenForm "FindPatientAmend", , , , , , "VaccinationAmend"
    Forms!VaccinationPrivate.Visible = False
 
nope - that does not work either I really cannot think as to why the mainform it is not hiding but (on the last bit of code the Subform is hiding)

I'm tottally lost !

thanks
 
The form doesn't have any odd settings like modal / popup / etc?
 
No its just a 'bog' standard form

it is opened based on two different 'openargs' but I can see that being the reason can it?
 

Users who are viewing this thread

Back
Top Bottom