MS Access DLookup Bound textbox formatting - vba

I have a basic form with some controls on it.
When I select a value in a combobox it fires a changed event. The changed event contains a Dlookup as follows
Me.Description = DLookup("[Description]", "[Lookup_Endorsements]", "[Code]= '" & Me.End_No & "'")
This works and populates the bound textbox "Me.Description". However it doesn't retain the formatting. If I set the value in an UnBound textbox it retains the formatting correctly.
For example,
Lets say the text is like this...
"The text is some sentences and then some returns with
i) an item here
ii) another here
then another sentence"
In a bound textbox the data return from DLookup is
"The text is some sentences and then some returns with i) an item here
ii) another here then another sentence"
It doesn't retain the original formatting. I have check the field in both the source and destination tables and they are like for like.
Any help much appreciated as I'm new to this sort of stuff in Access.

Related

VBA to select Listbox value based on table value

I'm certain this is a basic issue, but I can't find an answer. It may be wording my question poorly.
I have the listbox on a form linked to a table. On that table I have a field called "Functional Area." This field automatically gets populated based on the user's office symbol.
The listbox lists all of the functional areas.
I want the listbox to automatically start with the user's functional area selected. The user can select a different functional area from there.
This is my code, it works, but obviously selecting the 0 value of the listbox doesn't get me what I'm looking for.
'Detect Office
Me.Office = DLookup("[Office]", "*Local - User List - Active Directory", "[UserID] =" & "'" & Me.UserID & "'")
'Detect Function
Me.Function = DLookup("[Functional Area]", "0 - Active User - Funtional Area")
Me.[Functional Area] = Me.Function
'Set Listbox Value
Me!LetterSourceListBox.Selected(0) = True
Me.Refresh
Is there a way I can find the row number of the item in the listbox and then select that?
Thank you,
-Stephen
Per June7's comments, I just needed to make the functional area a bound field and everything works nicely!
Cheers!

How to select table values from combobox selection?

I'm using an Access 2003 database and have 2 comboboxes I am trying to work with. The first box I have perfected already, which is a dropdown of different tables (categories of parts). Once that table is selected, I want to be able to look at the part numbers within that category through a dropdown box selection. From here I want to be able to pull up the correct report for that category with that part number in it so I can print a report for every part number. I'm sure I'll have to write some sort of VBA, Query or Macro AfterUpdate() code, but I just don't know how to fill that second combobox with the selected table's part numbers.
Click here for an image of my Menu layout
Here's my Query for the first box to show the tables I want:
SELECT Msysobjects.Name
FROM Msysobjects
WHERE (((Msysobjects.Name) not Like "MSYS*"
And (Msysobjects.Name) not like "_*"
And (Msysobjects.Name) not like "~*"
) AND ((Msysobjects.Type)=1))
ORDER BY Msysobjects.Name;
And I think this is what I'll need to print after the second box has it's selection:
Private Sub partnumberselect_AfterUpdate()
DoCmd.OpenTable Forms![_Datasheet Printing].Form.TagLabelSelection.Column(1), acViewNormal
End Sub
Thank you in advance and let me know if you have any questions.
You are attempting what are called "cascading comboboxes" which means the second box is dependent on the selection of the first.
This is accomplished through the control source of the second combo box.
The first thing you should do is write a query that returns all possible options of the second combobox, without caring so much about filtering it based on the first combo selection. Once you have it returning the correct data, you will add a WHERE clause to the second box's control source that's something like:
WHERE Msysobjects.Name Like Forms![_Datasheet Printing]!TagLabelSelection.Value
This is referencing your first combobox on your form. So after a selection is made in the first combobox, the underlying control source of the second will have the proper criteria to return the appropriate options.
However, you will need to add some VBA to the AfterUpdate() event on the first combobox. Once the selection is made, you need the second box to refresh the control source to populate the correct selections. The code is simply:
Forms![_Datasheet Printing]![MySecondComboboxName].Requery
Please see the example below.
Private Sub cboCountry_AfterUpdate()
On Error Resume Next
cboCity.RowSource = "Select tblAll.City " & _
"FROM tblAll " & _
"WHERE tblAll.Country = '" & cboCountry.Value & "' " & _
"ORDER BY tblAll.City;"
End Sub
You can read all about this concept, and others, in the link below.
http://www.fontstuff.com/access/acctut10.htm

MS Access VBA changing TextBox filled by a ComboBox

i got a question about some "simple" MS Access vba. I got a ComboBox. When an item is selected, various TextBoxes are updated. Now i want to edit the text that is put in a TextBox after an item is selected (Because the data is filled with a very much spaces, so i want them out).
I tryed "before update", "after update", "on dirty" and "on change" events, but none of them seems to be the right one.. Anyone knows what i'm looking for? Thanks
Two possible ways:
(1) Instead of just entering the field name for each text box's ControlSource property, enter an expression, e.g. =Replace([SomeField], " ", ""). This assumes the text boxes are supposed to be read-only.
(2) Don't use data binding at all, and instead set each text box's Value property explicitly:
Dim RS As DAO.Recordset
Set RS = CurrentDB.OpenRecordset(SQL)
txtWhatever.Value = Replace(RS!SomeField, " ", "")
txtAnother.Value = Replace(RS!AnotherField, " ", "")
In the second case updating the data source with any altered values will need to be handled explicitly too.
That said, if the data has unnecessary spaces, surely they should be cleaned up at source, rather than as the data is put into the UI...?

ms-access: difference between .value and .text [duplicate]

I am passing the textbox1.text values into a query and sometimes into a string:
Dim combor1 As String
combor1 = comboReason1.Text
How do I know when I should put combor1 = comboReason1.Value?
Also, why do I need to set focus for a control to reference its property? That doesn't make sense to me.
Also, when I set combor4 = comboReason4.Value and the .value is null, then I get an error about invalid use of null.
".text" gives you what is displayed
on the screen
".value" gives you the underlying
value
Both usually give the same result, except when the corresponding control is
a combobox or listbox control
the displayed value differs from the bound column
Example:
id_Person is a combobox control in a form
the rowsource is "SELECT id_Person, personName FROM Tbl_Person"
column widths are "0cm;3cm"
bound column is 1
In this situation:
id_Person.text displays Tbl_Person.personName
id_Person.value displays Tbl_Person.id_Person.
.text property is available only when the corresponding control has the focus.
.text is a string value, therefore it cannot be Null, while .value can be Null
EDIT: .text can only be called when the control has the focus, while .value can be called any time ...
You can use the Text property to set or return the text contained in a text box or in the text box portion of a combo box.
To set or return a control's Text property, the control must have the focus, or an error occurs. To move the focus to a control, you can use the SetFocus method or GoToControl action.
You can use the Value property to determine or specify if a control is selected, the selected value or option within the control, the text contained in a text box control, or the value of a custom property.
The Value property returns or sets a control's default property, which is the property that is assumed when you don't explicitly specify a property name. In the following example, because the default value of the text box is the value of the Text property, you can refer to its Text property setting without explicitly specifying the name of the property.
Forms!frmCustomers!txtLastName = "Smith"
Text Property Reference
http://msdn.microsoft.com/en-us/library/aa173453.aspx
Value Property Reference
http://msdn.microsoft.com/en-us/library/aa173476.aspx
.text starts the field validation and causes an error if field validation is hurt. .value doesn't start the field validation, you may enter ANY value
This thread and the answers herein explain the issue well. There are a couple of additional points I'd like to add, which I've found through experimentation:
The order of precedence of the properties is:
.ControlSource
.Value
.Text
From what I've been seeing in Access 2007, if .ControlSource is undefined when the form opens, .Value will be Null.
If you set the .ControlSource property to ="" (an empty string), that will cause the .Value property to default to that instead of Null.
You can set the .Value property to "" in the Form_Load event. But...I've been seeing some erratic operation there; it seems as if .Value sometimes changes from "" back to Null, and I haven't yet worked out the circumstances.
So it seems best to define .ControlSource to ="", either in Design View or in the Form_Load event. But be forewarned, that niblet is tricky because of the embedded double quotes, and it can be tricky to read.
Some ways to do it are:
myTextbox.ControlSource = "=" & """"" (five double quotes in a row)
myTextbox.ControlSource = "=" & Chr(34) & Chr(34)
Etc, etc, there are many ways to do it...
Also, here's an extended tidbit. If you set the .TextFormat property to Rich Text, you can format the text in it with bold, italic, colors, etc. But be forewarned (again), beginning with Office 2007, the original Microsoft RTF format was decommissioned in favor of a "mini" version of HTML that only supports a few tags related to formatting fonts and paragraphs.
As an example, say you want the textbox to display the little ASCII checkbox character with the word "valid" in italics next to it, and make it all green. You can do it, but it all has to be in HTML, and it's not easy to read:
myTextbox.TextFormat = acTextFormatHTMLRichText
myTextbox.ControlSource = "=" & Chr(34) & "<font color=#80CA45><font face=Wingdings>" & _
Chr(254) & "</font> <font face=Calibri><i>Valid.</i></font></font>" & Chr(34)
If the text box is a ReadOnly control, the value property will not be used but if you set the text peoprty, the value will still be used in form data.

Distinction between using .text and .value in VBA Access

I am passing the textbox1.text values into a query and sometimes into a string:
Dim combor1 As String
combor1 = comboReason1.Text
How do I know when I should put combor1 = comboReason1.Value?
Also, why do I need to set focus for a control to reference its property? That doesn't make sense to me.
Also, when I set combor4 = comboReason4.Value and the .value is null, then I get an error about invalid use of null.
".text" gives you what is displayed
on the screen
".value" gives you the underlying
value
Both usually give the same result, except when the corresponding control is
a combobox or listbox control
the displayed value differs from the bound column
Example:
id_Person is a combobox control in a form
the rowsource is "SELECT id_Person, personName FROM Tbl_Person"
column widths are "0cm;3cm"
bound column is 1
In this situation:
id_Person.text displays Tbl_Person.personName
id_Person.value displays Tbl_Person.id_Person.
.text property is available only when the corresponding control has the focus.
.text is a string value, therefore it cannot be Null, while .value can be Null
EDIT: .text can only be called when the control has the focus, while .value can be called any time ...
You can use the Text property to set or return the text contained in a text box or in the text box portion of a combo box.
To set or return a control's Text property, the control must have the focus, or an error occurs. To move the focus to a control, you can use the SetFocus method or GoToControl action.
You can use the Value property to determine or specify if a control is selected, the selected value or option within the control, the text contained in a text box control, or the value of a custom property.
The Value property returns or sets a control's default property, which is the property that is assumed when you don't explicitly specify a property name. In the following example, because the default value of the text box is the value of the Text property, you can refer to its Text property setting without explicitly specifying the name of the property.
Forms!frmCustomers!txtLastName = "Smith"
Text Property Reference
http://msdn.microsoft.com/en-us/library/aa173453.aspx
Value Property Reference
http://msdn.microsoft.com/en-us/library/aa173476.aspx
.text starts the field validation and causes an error if field validation is hurt. .value doesn't start the field validation, you may enter ANY value
This thread and the answers herein explain the issue well. There are a couple of additional points I'd like to add, which I've found through experimentation:
The order of precedence of the properties is:
.ControlSource
.Value
.Text
From what I've been seeing in Access 2007, if .ControlSource is undefined when the form opens, .Value will be Null.
If you set the .ControlSource property to ="" (an empty string), that will cause the .Value property to default to that instead of Null.
You can set the .Value property to "" in the Form_Load event. But...I've been seeing some erratic operation there; it seems as if .Value sometimes changes from "" back to Null, and I haven't yet worked out the circumstances.
So it seems best to define .ControlSource to ="", either in Design View or in the Form_Load event. But be forewarned, that niblet is tricky because of the embedded double quotes, and it can be tricky to read.
Some ways to do it are:
myTextbox.ControlSource = "=" & """"" (five double quotes in a row)
myTextbox.ControlSource = "=" & Chr(34) & Chr(34)
Etc, etc, there are many ways to do it...
Also, here's an extended tidbit. If you set the .TextFormat property to Rich Text, you can format the text in it with bold, italic, colors, etc. But be forewarned (again), beginning with Office 2007, the original Microsoft RTF format was decommissioned in favor of a "mini" version of HTML that only supports a few tags related to formatting fonts and paragraphs.
As an example, say you want the textbox to display the little ASCII checkbox character with the word "valid" in italics next to it, and make it all green. You can do it, but it all has to be in HTML, and it's not easy to read:
myTextbox.TextFormat = acTextFormatHTMLRichText
myTextbox.ControlSource = "=" & Chr(34) & "<font color=#80CA45><font face=Wingdings>" & _
Chr(254) & "</font> <font face=Calibri><i>Valid.</i></font></font>" & Chr(34)
If the text box is a ReadOnly control, the value property will not be used but if you set the text peoprty, the value will still be used in form data.