Home > other >  How do I branch in Forms?
How do I branch in Forms?

Time:01-22

I'm creating a Form in MS Access. I have two fields called "CIP/MYOP NO:" and "If Yes, Contract Number:" I used the Lookup Wizard to create a Yes/No answer choice for both fields. When a user chooses "Yes" for "CIP/MYOP NO:" a field requiring an attachment should appear. When selecting "Yes" for "If Yes Contract Number:," a text field should appear. When selecting "No" it should skip to the next field in the form.

How would I go about creating something like this? I tried creating a Macro, by using If/Else and GotoControl but kept receiving a Data Type error. I'm not knowledgable on the syntax or macros and Google hasn't gotten me closer to solving the problem. If anyone could help out, I would appreciate it.

CodePudding user response:

Do the following.

  1. Set the visible property of both the attachment field and textbox to No This you can do using the property window.

  2. In Vba write the following to trigger the controls in 1 above to either be visible or not based on whether yes or no is ticked in the. i have used a yes/no data type, where checked =yes, and not checked =no. You will be writing three codes under three events which are.

A. The after update event of CIP/MYOPNO control B. The after update event of YesContractNumber control C. The forms Current event(this is to ensure the code in A and B is triggered based on the values in the active record and does not affect another record.

    Private Sub CIP_MYOPNOYES_AfterUpdate()
    If Me.CIP_MYOPNOYES.Value = -1 Then
    Me.ATTACHMENT.Visible = True
    Else
    Me.ATTACHMENT.Visible = False
    End If
    
    End Sub

     Private Sub CONTRACTNUMBERYES_AfterUpdate()
        If Me.CONTRACTNUMBERYES.Value = -1 Then
      Me.CONTRACTNOSTEXTFIELD.Visible = True
      Else
      Me.CONTRACTNOSTEXTFIELD.Visible = False
      End If
     End Sub




     Private Sub Form_Current()
         If Me.CIP_MYOPNOYES.Value = -1 Then
      Me.ATTACHMENT.Visible = True
      Else
      Me.ATTACHMENT.Visible = False
      End If
    
       If Me.CONTRACTNUMBERYES.Value = -1 Then
      Me.CONTRACTNOSTEXTFIELD.Visible = True
      Else
    Me.CONTRACTNOSTEXTFIELD.Visible = False
    End If
    
    End Sub

I have attached two images that show Property window showing the visible property set to No it.

VBA Code

  •  Tags:  
  • Related