Copy text box to table.

dbazzelle

New Member
Joined
Dec 4, 2003
Messages
38
Hello,

I have a form with 20 text boxes on it. They are all unbound boxes that I fill in with vba code. What I want to do is take textbox1, textbox2, current user name, amd the date/time and add them to a table as a new record. Then take textbox3, textbox4, and the other information and add that as a new record to the table. And doing the same with the rest of the textboxes in pairs. The table will be set up with a autonumber for primary key.

Thank you,
David Bazzelle
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try somethin like this:

Code:
Private Sub Command1_Click()
Dim rst As DAO.Recordset
Dim db As Database
Dim I As Integer

Dim ctrl As Control
Dim txt1, txt2
Set db = CurrentDb

Set rst = db.OpenRecordset("Table1")

For I = 1 To 20 Step 2

txt1 = "Text" & I

txt2 = "Text" & (I + 1)

With rst
    .AddNew
    !DateStamp = Now()
    !UserName = "User"
    !Item1 = Me.Controls(txt1)
    !Item2 = Me.Controls(txt2)
    .Update
End With

Next I

End Sub

This assumes that Table1 exists with fields ID, DateStamp, UserNAme, Item1 and Item2.
 
Upvote 0
Thank you, that was just about what I needed. Can you help with one more thing. When one of the text boxes is empty I would like it to goto the next one. Thank you for the help.

David Bazzelle
 
Upvote 0
I'm not quite sure what you mean.

Do you mean that if TextBox1 is empty then put the value of TextBox2 into field Item1? And so on...
 
Upvote 0
If box1 had nothing in it, then go to box3.. Or more like if box7 has nothing in it goto box9.

David Bazzelle
 
Upvote 0
What about this:

Code:
Private Sub Command1_Click()
Dim rst As DAO.Recordset
Dim db As Database
Dim I As Integer

Dim ctrl As Control
Dim txt1, txt2
Set db = CurrentDb

Set rst = db.OpenRecordset("Table1")

For I = 1 To 20 Step 2

txt1 = "Text" & I

txt2 = "Text" & (I + 1)
If Me.Controls(txt1) <> "" Then

With rst
    .AddNew
    !DateStamp = Now()
    !UserName = "User"
    !Item1 = Me.Controls(txt1)
    !Item2 = Me.Controls(txt2)
    .Update
End With
End If
Next I

End Sub
 
Upvote 0
Thank you, that looks like what I needed. I will let you know tomorrow if it works for me. Looks like it should though. Thanks again!

David Bazzelle
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,327
Members
451,697
Latest member
pedroDH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top