Auto insert area code - ms-access

I have a text box in Microsoft Access 2013 that corresponds to a telephone number. The text box has the input mask of:
!\(999") "000\-0000;;_
I am trying to auto insert the area code. For example, when a users goes to enter a telephone number, the area code is already populated into the text box. I have tried to set the default value in both the table design and on the property sheet of the text box but no luck. Its always aligned to the right. For example it looks like this:
If I add spaces to the default value on the text box property, the input mask is ignored and Microsoft Access generates an error stating:
The value you entered isn't appropriate for the input mask '!(999") "000-0000;;_' specified for this field.
I have contemplated modifying the table structure to include a specific field for an area code, but that is my last resort.
Is there a more efficient way to automatically populate the area code in the text box when a new record is generated?

You can write a function in VBA to do this. Something like;
Function FormatPhone(PhoneNumber As String) As String
FormatPhone = "(719)" & PhoneNumber
End Function
This function will take a string like '123-4567' and return '(719)123-4567'.
In the AfterUpdate event of your phone number field put;
[TextBoxControlName].Value = FormatPhone(Nz([TextBoxControlName].Value,""))
and this will add the formatted string to the text box.

Related

Retrieving value including input mask from TextBox field - Access VBA 2016

I have an input mask of JAAA-AAA on a TextBox, so when the user enters that input field, they will see this: "J___-___".
However, when they fill it out like so, J123-321, and I get the value using [myfield].Value, it says that the value is only 123321. It strips the preset 'J' and '-' I had in there. How can I prevent the stripping of these characters when I retrieve the value from this field?
You need to set the second part of the input mask property to 0.
If you look at the documentation, you can see it has 3 parts, and the 2nd part controls if the mask is stored with the data, or only the data is stored.
The final mask property would be the following:
\JAAA\-AAA;0;_

Multiline property problems

I have a column in my database that is set to memo. I am trying to view the data in a textbox.
I have enabled new line in field from the Enter Key Behaviour property but all the data from the record is now showing - What am I missing?
The data is being pulled from a list box, example code below:
Textbox1 = listbox.column(1)
Thanks in advance
This has nothing to do with the EnterKeyBehavior property of the text box.
http://allenbrowne.com/ser-63.html
Row Source
A Memo field in the Row Source of a combo box or list box will
truncate.
Don't use memo fields in combos or list boxes.
You'll need a different method to load the text box, e.g. read the ID from the listbox and use DLookup().

Putting code in a text box (MS Access)

I am using a textbox for the header of my report, and based on what the user selects it will be "Baseline 8", "Baseline 9", etc. What I would like to do is have the text box coded so whatever number the user selects is entered into the text box. I managed to do it by using two text boxes, one just says "baseline" and the other text box says "=[Forms]![Navigation Form]![NavigationSubform]![Combo21]" and it will enter the correct value. But what I want to do is put it all in one box, and when I put "Baseline =[Forms]![Navigation Form]![NavigationSubform]![Combo21]" in the text box it doesn't work, it just leaves the code as the header when I generate the report. Is there something I'm not doing correctly?
First of all, when you state that a "textbox says", you really mean that "the Control Source property of the textbox equals." For a textbox (and some other controls), the value that you see on the actual form IS the Control Source property. I am not being picky for its own sake, rather it is important to recognize what value you are editing.
The Control Source property can essentially contain two types of values. The first is without an equals sign and it indicates the name of a field from the form Record Source. In that case, it binds the control to the field directly so that it automatically loads from the field and saves changes back to the field.
The second type of value always starts with =. It is a VBA code expression and can include calls to functions and other VBA operators. In your case, you want to concatenate (i.e. combine) two strings: one literal "Baseline" and one pulled from an access object [Forms]![Navigation Form]![NavigationSubform]![Combo21], so you need to use the string concatenation operator &.
="Baseline " & [Forms]![Navigation Form]![NavigationSubform]![Combo21]

copy value entered in unbound textbox to label after pressing button

I have an unbound text box on my form which the admin can enter a date of when records were last updated. I want this date to be copied to a label after pushing a button so that it holds the date instead of it do disappear after closing the form.
does anyone know if this is possible and how it is possible?
For creating a variable that can be used AFTER the form is closed:
Create a new module (in code window, click 'Insert | Module'
Insert the variable name(s) and types you want available everywhere. i.e.
Global dtLastUpdated As Date
Global strSaveSomeName As String
Save the module as mdl_Globals
Add code wherever needed to set the variable, then can reference anywhere.
If for use during the form, use the following: where 'lblForUnbound' is the Label field and 'txtUnbound' is your unbound text box
Me.lblForUnbound.Caption = Me.txtUnbound.Text

How do I substitute one entry for another an MS Access report?

I recently started working with MS Access 2010, and I am trying to generate labels from a form that I have created. In the form, three pieces of information are put in by the user: style, color code, and unit of measure (UoM). The style numbers appear the same way on the form and in the report, and I have been able to get that to work.
However, for the color code, I need both the inputted color code and the actual color to show up on the report. I have a table that has all of the color codes with the corresponding color names. I cannot figure out how to get the text box that is supposed to show the color name to show it. I know virtually no SQL, but I found information on it on the internet and pieced together this code in the ControlSource for the text box the color name is supposed to be in:
=(SELECT [Description]
FROM [Color]
WHERE([Forms]![Box Label Form]![ThirdJoined]=[Color]![ColorCode]))
[Description] is the name of the column within the [Color] table that gives the actual color name.
[Box Label Form] is the name of the form.
[ThirdJoined] is the name of the input text box within the form.
[ColorCode] is the name of the column within the [Color] table that gives that color code.
This code doesn't work, and only results in #NAME appearing in Print Preview view. How can I get this to work, either code-wise or otherwise?
You cannot set the ControlSource of a textbox to a SQL statement. You could set it to a DLOOKUP function to lookup a single value. You also need to separate out the reference to the form control using concatenation (&).
=DLOOKUP("Description","[Color]","ColorCode='"&[Forms]![Box Label Form]![ThirdJoined]&"'")
I'm assuming the Color is a text-value so the form-value needs to be enclosed in apostrophes.
If this expression is used on the form [Box Label Form] then you don't need to qualify the name of the Control:
=DLOOKUP("Description","[Color]","ColorCode='"&[ThirdJoined]&"'")