470,849 Members | 1,228 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

How can I set up a command button to ask for input to change the default value of a text box in an Access form?

What I am trying to do is to change the default value using a command button. We have over 200 work centers and I want to be able to change it on the fly (so to speak). Here are the 2 lines of code that I have been trying to get one of them to work:

Forms!frmMainentry!txtStation.DefaultValue = """?"""

Me.txtStation.DefaultValue = InputBox [What Workcenter Do You Want To Change Too?]

The first one works, but to change the value, I have to change it in the code.
The second one give me an error:

Compile error: Expected end of statement.

I've been working on this with researching the internet, my Access Bible just trying different things for 5 days.
It will be on a computer at work, but both mine and at work are running Windows 10, at home I am using Access Pro 2010 at work we are using Access 2016.
If you need more info, let me know and I will give what I have and will be checking here daily.
Any help you can give would be GREATLY appreciated.
Jun 5 '21 #1
1 2298
32,311 Expert Mod 16PB
DefaultValue properties can be awkward. Generally speaking it has to be a string value that evaluates to an assignment. So, assuming a Workcentre (Feel free to change spelling if an American cousin ;-) ) has something like a three-character code (EG. CRO or BRI.) then an assignment in code could be something like :
Expand|Select|Wrap|Line Numbers
  1. Me.txtStation = "CRO"
Note the quotes. In that case the DefaultValue value would have to include the quotes too :
Expand|Select|Wrap|Line Numbers
  1. Me.txtStation.DefaultValue = "CRO"   ?
No. That is simply the string itself. The quotes are interpreted by VBA to enable it to recognise the string as a string. The resultant string is simply CRO. Not good enough. Works OK with a number but not with strings.

What you need there, bearing in mind that quotes within a string need to be doubled up in order to be recognised as quotes within the string rather than the end of the string, is :
Expand|Select|Wrap|Line Numbers
  1. Me.txtStation.DefaultValue = """CRO"""
Here's how VBA interprets each quote :
"#1 Start of a string.
"#2 End of string or start of a pair?
"#3 #2 was clearly one of a pair so insert a single double-quote in next position of string.
"#4 End of string or start of a pair?
"#5 #4 was clearly one of a pair so insert a single double-quote in next position of string.
"#6 End of string or start of a pair?
End of string.
That leaves you with a string of "CRO", which should work.

All well & good, but how to code that?
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFixWC()
  2.     Dim strWork As String
  4.     strWork = InputBox("Which Workcentre do you want to Change To?")
  5.     Me.txtStation.DefaultValue = Replace("""%WC""", "%WC", strWork)
  6. End Sub
Jun 5 '21 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

By using this site, you agree to our Privacy Policy and Terms of Use.