Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reset unbound text box to default value in VBA?

Status
Not open for further replies.

WaltW

MIS
Jun 14, 2000
130
US
I have a form where I need to use VBA to reset an unbound text box to its default value after the info in the form has been processed. I've tried statements like "Me.txtField=Me.txtField.DefaultValue", but I get the literal value of what's in the default value field. For example, in a year field instead of the current year as a number I get "Year(Date())" in the field. Or for text fields I get the default text in quotes. How can I make this work right? Thanks for any help offered!

Walt
 
Guess I didn't explain very clearly. The DefaultValue property of the txtField text-box control contains: Year(Date())

I don't want to have to specifically reset txtField value to Year(Date()). I want to be able to reset the txtField value to whatever is in the DefaultValue property for that field. Does that make sense?

Thanks.

Walt
 
For a simple attempt, and just to make sure, the default value is
="Choose One"
or its equivalent right? I am checking to make sure the equal sign is there basically for the default.

If that does not work, I will post some code that gets the job done, along with my disclaimer when it goes to get posted =P

~
Give a some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Ok, since my day is almost over anyways, and to help out sooner rather than later, I am going to post the code stuffs anyways. My disclaimer is that this is not my code really. I replaced the guy who was here before me since he moved on. He made most of this MS Access DB program. I have not coded at all in VB until 2 months ago when I got this job, and that first month was spent learning about VB. I have coded in other languages for 5 years though so I can look at VB and understand most of it.
Here is the code in its pieces:

Public Property Get strtxtref() As String
strtxtref = "cmbpgmref, cmbfinref, cmbaccref, cmbdesref, cmbclrref, cmbeworef, cmbprcref, cmbamtref"
End Property

Those cmbpgmref and others are the combo boxes' names that are going to be reset.

Private Function setdefault(str As String)
Do While Len(str) > 0
Form_FormNameHere(clsaction.firstcon(str)).Value = _
clsaction.formatstring(Form_FormNameHere(clsaction.firstcon(str)).DefaultValue())
str = clsaction.concat(str)
If Mid(str, 1, 3) = "cal" Then
str = clsaction.concat(str)
End If
Loop
End Function

This is called with the line "setdefault strtxtref" to pass it the names of the fields to be reset.
The other subfunctions called from a module:

Public Function firstcon(str As String) As String
Dim spot As Integer
spot = InStr(str, ",")
If spot = 0 Then
firstcon = str
Exit Function
End If
firstcon = Mid(str, 1, spot - 1)
End Function

AND

Public Function concat(str As String) As String
Dim spot As Integer
spot = InStr(str, ",")
If spot = 0 Then
concat = ""
Exit Function
End If
concat = Mid(str, spot + 2, Len(str) - 1)
End Function


Those from my understanding cycle through the combo boxes names to fix them one by one.

**It could just be that if that was your exact line of code, adding the () to the end of DefaultValue would work?

Sorry for the long post but I hope one of these ideas helps.
I would feel helpful back to the Tek-Tips community for a change with as much as I have learned from all of you over the past 2 months.

~
Give a some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Ohp, sorry, forgot to mention,
It is all VBA inside of MS Access 2K.

~
Give a some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top