Unexpected behaviour when assign value to "Default Value" property (1 Viewer)

ADIGA88

Member
Local time
Today, 03:30
Joined
Apr 5, 2020
Messages
94
hi guys

I have weird behavior when i am tring to assign value from textbox to another textbox default value, the example will make it clear.
Environment:
windows 10
office 365
access VBA

Code:
' this is not working
Me.Txt_Vendor_Name.DefaultValue = """ & Me.Txt_DefaultVendorName & """

' this is working just fine
Me.Txt_Vendor_Name.DefaultValue = "'" & Me.Txt_DefaultVendorName & "'"


the not working example just use Me.Txt_DefaultVendorName as text and put it as it's.
knowing multiple threads suggest the not working example.


1594673103707.png
 

CJ_London

Super Moderator
Staff member
Local time
Today, 01:30
Joined
Feb 19, 2013
Messages
16,610
all you need is

Me.Txt_Vendor_Name.DefaultValue = Me.Txt_DefaultVendorName

note that default value will only populate for new records - if the record already exists, although the defaultvalue property will be populated, it will not appear in an existing record.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:30
Joined
Feb 19, 2002
Messages
43,233
You don't have enough double quotes:

Me.Txt_Vendor_Name.DefaultValue = """" & Me.Txt_DefaultVendorName & """"

You need four in this case. The outer two enclose the literal. Since the literal is a delimiter it needs to be "escaped" and so it must be entered twice.

In order to not have to deal with reading bunches of quotes, I create a global constant named QUOTE:
Code:
Option Compare Database
Option Explicit

Public Const QUOTE = """"

I keep a standard module where I define all public variables and TempVars so they are all located in one place. Using the constant, the expression would be:
Me.Txt_Vendor_Name.DefaultValue = QUOTE & Me.Txt_DefaultVendorName & QUOTE
 

ADIGA88

Member
Local time
Today, 03:30
Joined
Apr 5, 2020
Messages
94
all you need is

Me.Txt_Vendor_Name.DefaultValue = Me.Txt_DefaultVendorName

note that default value will only populate for new records - if the record already exists, although the defaultvalue property will be populated, it will not appear in an existing record.
In this case both date and text textboxs is not working
1594718483626.png



You don't have enough double quotes:

Me.Txt_Vendor_Name.DefaultValue = """" & Me.Txt_DefaultVendorName & """"

You need four in this case. The outer two enclose the literal. Since the literal is a delimiter it needs to be "escaped" and so it must be entered twice.

In order to not have to deal with reading bunches of quotes, I create a global constant named QUOTE:
Code:
Option Compare Database
Option Explicit

Public Const QUOTE = """"

I keep a standard module where I define all public variables and TempVars so they are all located in one place. Using the constant, the expression would be:
Me.Txt_Vendor_Name.DefaultValue = QUOTE & Me.Txt_DefaultVendorName & QUOTE

Code:
If Not IsNull(Me.Txt_DefaultDate) Then Me.Txt_Attendance_Date.DefaultValue = "'" & Me.Txt_DefaultDate & "'"
If Not IsNull(Me.Txt_DefaultVendorName) Then Me.Txt_Vendor_Name.DefaultValue = """" & Me.Txt_DefaultVendorName & """"

the four quote make sense for the need of escaping the literal, but why the second case is working " ' " & variable & " ' "
I will go with 4 quotes for now as the second case maybe failed in some exception.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:30
Joined
Feb 19, 2002
Messages
43,233
"'" works because ' isn't the string delimiter. '"' might work because the delimiter is ' rather than ". To embed a single quote and use single quotes for the delimiter, you still need four of them. ''''
 

Users who are viewing this thread

Top Bottom