Subform visibility based on textbox value

MCCDOM

Registered User.
Local time
Today, 08:22
Joined
Oct 30, 2014
Messages
84
Hi there,

I am trying to get a subform to be hidden when the value of a textbox (txtAllocationID) is empty but when a ID number appears I would like it to become visible again.

This is what I have tried so far but doesn't seem to work:
Code:
Private Sub txtAllocationID_AfterUpdate()

    If Me.txtAllocationID.Value >= 0 Then
        Me.frmStockAllocated.Visible = True
    Else
        Me.frmStockAllocated.Visible = False
    End If
    
End Sub

Am I in the right ball park or have I gone about this the wrong way?

My allocation ID is a primary key so all auto number generated and only an integer.

Please find attached images of my form with when I want it to be hidden and when I want it to be visible.

Scenario: When I enter a stock ID of 121 and search I want the subform to remain hidden as there is no Allocation ID related to that Stock ID (see attachment tblAllocated). If I was to enter a stock ID of 122 I want the subform to become visible as the stock ID has a relation with the Allocation ID 6 and therefore the textbox txtAllocation ID would display '6' in it.

Hope this makes sense and look forward to your suggestions.
 

Attachments

  • StockFinderForm Overview.JPG
    StockFinderForm Overview.JPG
    76 KB · Views: 149
  • SubFormVisibleFalse.JPG
    SubFormVisibleFalse.JPG
    24.2 KB · Views: 156
  • SubFormVisibleFalse2.JPG
    SubFormVisibleFalse2.JPG
    29.2 KB · Views: 134
  • SubFormVisibleTrue.JPG
    SubFormVisibleTrue.JPG
    31 KB · Views: 147
  • tblAllocated.JPG
    tblAllocated.JPG
    33.2 KB · Views: 150
In your textbox afterUpdate event put:
If trim(me.Yourtextbox & "") = "" then
Me.Subform.Visible = false
Else
Me.Subform.Visible = true
End if

You can do it in the short code:
Me.Subform.Visible = not (trim(me.Yourtextbox & "") = "")

Put this code also in your mainform onOpen event.
 
Thanks Smig,

That didn't seem to work for me. Got an error saying "Compile error: Method or data member not found." This is being flagged up in the mainform as it can't find the textbox on the subform.

Would you say this is the best method of doing this, using a value greater than 0 to change the visible value, or are there better ways?

Many thanks
 
Thought I'd better update this as it is working in my database. For the benefit of others looking to implement a similar setup I have used the following code in my main form:
Code:
Private Sub Form_Load()
    
'Make the Subform invisible upon opening of the main form
    Me.frmStockAllocated_Subform.Visible = False
    
End Sub

and then this code on my subform:
Code:
Private Sub Form_Current()
'If no value is present in txtFirstName then the subform becomes invisible
    Me.Form.Visible = Not IsNull(Me.txtFirstName)
    
End Sub

Private Sub txtFirstName_AfterUpdate()
'If a value appears in txtFirstName then make the subform visible
    Me.frmStockAllocated_Subform.Visible = True
    
End Sub

Thanks again everyone for your help.

Kind regards,

Dom
 

Users who are viewing this thread

Back
Top Bottom