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

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

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!

MS-Access Form shows Number, not Name on load

I got a Database, where I apply to a name a Main and Subgroup.
When I enter a MainGroup f.e. Granades, just subgroup elements like "attack-granades" etc. should be shown.
In genereal it works by writing in this into the MainGroup-Combobox at my Form.
Private Sub MunHauptgruppeRef_AfterUpdate()
Me.MunUntergruppeRef.Requery
Me.MunUntergruppeRef.RowSource = " SELECT UnterGrpNR, UnterGrpName FROM tbl_UnterGruppen WHERE UnterHauptGruppenNr = " & MunHauptgruppeRef.Value & " ORDER BY UnterGrpName ASC"
The Problem is, if I load the datas in my form again, it just shows the related Number to the "Sub-Combobox data" 1( f.e. 35 for Attack-Grenade ) , but not the Name itself. After I reselect the entry in my Main-Combobox(Grenade), it shows the right sub-data which was saved. 2
Tried Requery on Form_Load or Requery of the Combo-Boxes itself. nothing helped so far.
Made some Video3
You need to add the code from Private Sub MunHauptgruppeRef_AfterUpdate() to your Form_Current event, this will refresh your combo box row source to the current value of your MunHauptgruppeRef combobox as you cycle through your records. Also you need to call the Me.MunUntergruppeRef.Requery after you've set the Me.MunUntergruppeRef.RowSource.
You can also add a check when your in a New Record, for the code not to run on the Current Event. See here https://learn.microsoft.com/en-us/office/vba/api/access.form.newrecord

MSACCESS - Closing Form edits 1st entry

I have table with 3 Columns. It has a AutoNumber Primary ID (cboID), A Text Column (txtLocation), and a Number Column (txtCount).
From this table, I made a form that has a combobox and two text fields. When the combobox is clicked, it shows a list of all the cboID's. After selecting one of the choices, it autopopulates the two other text fields with it's respective Location and Count info. I also have an update statement/save button that allows the user to edit one of the entries, press save, and update one or both the fields.
Here's the statement I have.
Private Sub cmdUpdate_Click()
Dim strSQL As String
strSQL = "UPDATE aDuh SET Location = '" & Me.txtLocation & "', Count = '" & Me.txtCount & "' WHERE ID = '" & Me.cboID & "'"
DoCmd.RunSQL (strSQL)
End Sub
The updating has no problems, but the form updates the 1st entry when I close it. Lets say I just finished editing the third entry and I'm done editing. I want to close the form and do something else. However, I close the form (I've tried putting a button and just closing the tab) and the 1st entry in the table is updated with the third entry. So basically, my last edited entry edits properly, but when the form is closed, it also updates the 1st record. Any idea of how to fix this? (If this isn't clear please let me know and I'll try to clarify)
I'm thinking of writing a statement for the On Close event but I'm not sure what to put.
EDIT: Extra Info
Right now, this table only has three records. In the form, when I click the combobox, it has the drop down list of the three records. Lets say I clicked the second one. After clicking it, the Location field is populated with the related info and same with the count field. Now lets say the current value in the count field is "9" and I want to change it to 4. I type in 4, press update and I'm prompted with a box saying "are you sure you want to update 1 row". I press yes, and it updates. Updating works fine, its just when I close. I'll give two examples
1) I just edited the second record, when I close, it doesnt prompt anything, it just closes smoothly, but when I check the information, the first and second record are now the exact same.
2) Lets say I just edited the first record. When I close it, a window pops up saying "Write Conflict, This record has been changed by another user since you started editing it. If you save the record, you will overwrite the changes". It then has three options, Save Record, Copy to Clipboard and Drop Changes.
Oh and how i'm making it autopopulate, I have the code:
Private Sub cboID_Change()
Me.txtLocation = Me.cboID.Column(1)
Me.txtCount = Me.cboID.Column(2)
End Sub
Hopefully this clarifies a bit more. :(

Writing values back to a form

I have a form called Main. On it I have
a text box to hold the record ID
a combobox to select from existing records or enter a name for the new record
a text box to hold the name from the combobox
a button to open a form that lets us enter additional data and create a new record
a button to open a form that lets us edit an existing record
I cascade the row source for the list in the combobox. If the entry is not in the list,
the NotInList event
Copies the name in the combobox is moved to a text box on Main
Loads the create form's and copies the name from Main to the appropriate field on the create form.
All that works fine. The problem is that when I hit Save on the create form, I cannot write values back to the Main form correctly. I want to write the name that came from the combobox back to the combobox on Main, and write the record ID to the text field on Main.
It's two lines of code. If I change their order, the output changes. Either the combobox ends up blank, or the record ID text field ends up blank.
While I was debugging this I can see the values exist, but I could not make the assignments. The last time I tried combobox.text it complained about the value not being
in the list, so I ran the query for the row source, but the record did not show up in the combobox list, but is in the table. See the two lines of code (bold) below.
Private Sub cmbSaveClose_Click()
Dim x As Integer
x = MsgBox("Are you sure you want to save changes?", 4, "Exit?")
'VbNo constant throws back #7
'VbYes constant throws back #6
Me.Txt32 = DLookup("InsuranceCarrierContractID", "ICCDupRecordCheckQ")
If x = 7 Then
Exit Sub
End If
If IsNull(Me.Txt32) Then
Me.Txt31 = Form_frmMain.Txt65
DoCmd.RunCommand acCmdSaveRecord
**Forms!frmMain.cboInsuranceCarrierContract = Me.InsuranceCarrierContractID
Forms!frmMain.Txt66 = Me.InsuranceCarrierContractID**
DoCmd.Close
Forms!frmMain!InsuranceCarrierContract.RowSource =
" SELECT InsuranceCarrierContract.ContractNumber " & _
" FROM InsuranceCarrierContract " & _
" ORDER BY InsuranceCarrierContract.ContractNumber;"
'Other stuff,works fine.
Form_frmMain.cboInsuranceCarrierContract.Locked = False
Form_frmMain.cboInsuranceCarrierContract.BackColor =
Form_frmMain.cboInsuranceCarrier.BackColor
Else
Me.Undo
DoCmd.Close
End If
End Sub
This worked for David, so adding it as an Answer:
ReQuery and ReFresh the form after you change the data.
Me.Requery
Me.Refresh
from within the form module, or externally:
Forms("MyFormName").Requery
Forms("MyFormName").Refresh
http://msdn.microsoft.com/en-us/library/office/ff191903.aspx
http://msdn.microsoft.com/en-us/library/office/ff836021.aspx

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