Using IIF statement for a command button "on-click" (1 Viewer)

cburgess

New member
Local time
Today, 17:46
Joined
Apr 6, 2016
Messages
4
I have a form with a command button. At the moment when I click the button it inserts text from the form onto another form. This all works fine.

I am now looking to extend the functionality of adding an IIF statement to this button so that I can use this one form to insert text to many different places based on a value.

My Problem
When I try to code the button with the IFF statement it says:
"Expected: line number or label or statement or end of statement" and it highlights the = sign at the beginning.

This is the code
=Iif([txt1] = "c", Forms![frmCompany]![C_Address1] = [Address1],"")

Any help please?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:46
Joined
May 7, 2009
Messages
19,229
you need VBA, use the click Event of the button.

private sub button_click()
select case me.txt1
case "c":
Forms![frmCompany]![c_address1] = me.[address1]
'add other case here
'case "b"
'case "a"
'this is the default
'case else
'
end select
end sub
 

cburgess

New member
Local time
Today, 17:46
Joined
Apr 6, 2016
Messages
4
Thank you, this works well..

One more thing - with the first case, it updates on the second form straight away but when I add the second case it only updates once the form is closed or refreshed etc.
Is there any reason for this?

My code now looks like this:

Private Sub Command17_Click()
Select Case Me.txt1
Case "C":
Forms![frmCompany]![C_Address1] = [Address1]
Forms![frmCompany]![C_Address2] = [Address2]
Forms![frmCompany]![C_Town] = [Address_Town]
Forms![frmCompany]![C_Post] = [Address_Post]
Case "R":
Forms![frmCompany]![R_Address1] = [Address1]
Forms![frmCompany]![R_Address2] = [Address2]
Forms![frmCompany]![R_Town] = [Address_Town]
Forms![frmCompany]![R_Post] = [Address_Post]
Case Else
'
End Select
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:46
Joined
May 7, 2009
Messages
19,229
Private Sub Command17_Click()
Select Case Me.txt1
Case "C":
Forms![frmCompany]![C_Address1] = [Address1]
Forms![frmCompany]![C_Address2] = [Address2]
Forms![frmCompany]![C_Town] = [Address_Town]
Forms![frmCompany]![C_Post] = [Address_Post]
'force save the form
Forms![frmCompany].Form.Dirty = False
Case "R":
Forms![frmCompany]![R_Address1] = [Address1]
Forms![frmCompany]![R_Address2] = [Address2]
Forms![frmCompany]![R_Town] = [Address_Town]
Forms![frmCompany]![R_Post] = [Address_Post]
'force save the form
Forms![frmCompany].Form.Dirty = False

Case Else
'
End Select
End Sub
 

cburgess

New member
Local time
Today, 17:46
Joined
Apr 6, 2016
Messages
4
Thank you for your assistance. I have ended up using the requery function:

Private Sub Command17_Click()
Select Case Me.txt1
Case "C":
Forms![frmCompany]![C_Address1] = [Address1]
Forms![frmCompany]![C_Address2] = [Address2]
Forms![frmCompany]![C_Town] = [Address_Town]
Forms![frmCompany]![C_Post] = [Address_Post]
Forms!frmCompany!.SetFocus
DoCmd.Requery
Case "R":
Forms![frmCompany]![R_Address1] = [Address1]
Forms![frmCompany]![R_Address2] = [Address2]
Forms![frmCompany]![R_Town] = [Address_Town]
Forms![frmCompany]![R_Post] = [Address_Post]
Forms!frmCompany!.SetFocus
DoCmd.Requery
Case Else
End Select

DoCmd.Close acForm, "frmAddress", acSaveNo

End Sub

Thank you!
 

Users who are viewing this thread

Top Bottom