MS Access VBA changing TextBox filled by a ComboBox - vba

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...?

Related

MS Access DLookup Bound textbox formatting

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.

How can I save/append text from one textbox to another textbox on the same Form in VB

Basically what I'm trying to figure out how to do is save text from one textbox to another textbox on the same form. I see a lot of post on how to save or store text from one textbox to another on a different form but not any with doing that on the same form.
I'm currently working on a feature in my program that allows me to with a click fn my "LogItLater" button save the fields from the First Name, Last Name, and Phone# textboxes on my form and place them on a different textbox on my form which allows the user to add more or edit that information at later time.
I'm doing this in VB so just a simple how to would be great, this is my first time asking a question so I hope I was as specific as possible.
So my new issue is this when I add more than one statement like this
txtScrapeBox.Text = txtFirstName.Text
txtScrapeBox.Text = txtLastName.Text
txtScrapeBox.Text = txtPhone.Text
The only text that is saved is the last statement and in this case it is the txtPhone.Text, will I need a condition statement to get the first two to save as well?
Only the last assignment is retained, because you are replacing the contents with each assignment. To append (I am assuming this is a multiline TB - if it isnt, the data will come out: ZiggyWagner(800)5550195):
txtScrapeBox.Text = txtFirstName.Text & Environment.NewLine & _
txtLastName.Text & Environment.NewLine & txtPhone.Text
This presents a problem down the road - if they want to edit "LastName", how will you know which line they want to edit? You will have to save all three items to txtScrapeBox in the same order every time and fetch them back using the .Lines() property.
Another way might be to save them to a ListBox where item 0 is always first name, item 1 = LastName etc:
lbData.Items.Clear ' remove previous contents
lbData.Items.Add(txtFirstName.Text)
lbData.Items.Add(txtLastName.Text)
lbData.Items.Add(txtPhone.Text)
Get one back to edit:
txtFirstName.Text = lbData.Items(0).ToString
' or
If lbData.SelectedItems.Count > 0 Then ' check if they selected one
txtFirstName.Text = lbData.SelectedItem.ToString
End If

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.

VBA for taking information from text boxes and inserting into table

So I have an input form that I want to use to update a table with certain fields of information. I have the ID of the record automatically coming up in a text box. I have three text boxes that I need to add to the table (excluding the ID) on a button click.
Name
Date
Method
are the field names.
As_Name
As_Date
As_Method
are the text box names
The table name is POC, and the ID is POC_ID (its an autonumber).
So I do not want these objects (text boxes) to be bound to the table because this is a little separate "pop-up" form that comes from a form that the table, and I only want the input to be relative to the POC_ID that is already selected via the original form.
So how do I write the vba for this to 1)check to make sure that records do not already exist....2)update the fields (listed above) with data input from the text boxes(listed above). I want to be able to use this with a button click....
EDIT:
actually it is one table not two; i have two forms that I want to be able to send information to the same table (different information though). this db was already built by someone else and know I have been deamed to take it over.
I need to add a second little pop up form for additional information to be added based on new requirements (there is literally no where for me to place this on the other one). I have already done that, and used a suggested object approach to reference the first forms (from which this second "pop-up" form springs from) to add the relative id fields. Now I have this second little pop up form that just asked three values to be inputted (which are the ones listed above).
I just simply do not know how to link the text box, with a field so that once a user enters in the information, clicks "save" it saves the information to the table relative to the TripID that is there (one mentioned above). the only way I know how to get the text boxes to save data to the table is to use the builder/wizard when I create a new one.
I would like to learn how to link an object (text box, cmb, list) etc on a form, to a table with an "On Click" method so that I can use a save button. Basically that is it!
The OpenForm method of DoCmd allows for several arguments, including Where and Openargs. You can take advantage of these.
However, something seems to be quite wrong with your table design in that you appear to be holding the same information in two tables and for no stated reason. Have you read http://www.r937.com/relational.html?
I would suggest that the design you need probably only includes a numeric field POC_ID that is a foreign key to the main table.
Still not sure I understand your situation, but let me offer the outline of an answer. If my outline is not close enough, please explain where I went astray.
Your parent form, frmParent, has a command button (cmdMoreFields) which opens the child form (frmChild) where you will enter values for 3 additional fields in the record currently displayed in frmParent. After the user enters those values in frmChild (in text box controls named As_Name, As_Date, and As_Method), she will click a command button (cmdSave) to store those values to fields (Name, Date, and Method) in table POC, and close frmChild. Also, frmParent includes a text box (txtPk_field) which holds the value for the primary key (field pk_field in table POC) of the current record.
However, I'm not sure which field/control you're using for txtPk_field, and doubt that value is available if the the current record has not yet been saved. So, I'll suggest this code for the cmdMoreFields "on click" event:
If Me.Dirty Then Me.Dirty = False
DoCmd.OpenForm "frmChild"
In the "on click" event of cmdSave (on frmChild), try code similar to:
Dim strSql As String
strSQL = "UPDATE POC SET [Name] = """ & Me.As_Name & """, [Date] =#" _
& Me.As_Date & "#, Method = """ & Me.As_Method & """ WHERE pk_field = " _
& Forms!frmParent.txtPk_field & ";"
Debug.Print strSql
CurrentDb.Execute strSql, dbFailOnError
DoCmd.Close
If that approach works, consider passing the pk_field value to frmChild with Openargs, as Remou suggested.
Note: I assumed the data type for Name is text, Date is date/time, and Method is text. You will have to change the UPDATE statement delimiters for any fields whose data types differ from my guesses.