If then statement in Visual Basic

S

ServiceEnvoy

I have created 2 buttons to open 2 different forms but I would like to
create one button to open a certain form based on certain criteria.

Here is my current code for one of the buttons:
Private Sub ContactsForm_Click()
On Error GoTo Err_ContactsForm_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FRM_Contacts-General"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ContactsForm_Click:
Exit Sub

Err_ContactsForm_Click:
MsgBox Err.Description
Resume Exit_ContactsForm_Click
End Sub

I would like the button to open the Form "FRM_Contacts-General" if the
field "SubType" = advertising, contact, depot, wholesaler, or
manufacturer.

I would like the button to open the form "FRM_Vendors-All" if the
field "SubType" = vendor or employee

How do I do this?
 
M

Marshall Barton

ServiceEnvoy said:
I have created 2 buttons to open 2 different forms but I would like to
create one button to open a certain form based on certain criteria.

Here is my current code for one of the buttons:
Private Sub ContactsForm_Click()
On Error GoTo Err_ContactsForm_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FRM_Contacts-General"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ContactsForm_Click:
Exit Sub

Err_ContactsForm_Click:
MsgBox Err.Description
Resume Exit_ContactsForm_Click
End Sub

I would like the button to open the Form "FRM_Contacts-General" if the
field "SubType" = advertising, contact, depot, wholesaler, or
manufacturer.

I would like the button to open the form "FRM_Vendors-All" if the
field "SubType" = vendor or employee


How about using:

If Me.SubType = "vendor" Or Me.SubType = "employee" Then
stDocName = "FRM_Vendors-All"
Else
stDocName = "FRM_Contacts-General"
End If
 
N

Naeem Azizian

Don't forget to use the same code for choosing the criteria if the
criteria field is different.

If Me.SubType = "vendor" Or Me.SubType = "employee" Then
stLinkCriteria = "[ID]=" & Me![ID]
Else
stLinkCriteria = "[ID]=" & Me![ID]
End If

If criteria is the same just escape this;)
 

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