By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

How to save unbound textbox to a table using command button?

P: 1
i am complete newbie in ms access 2007. I want to ask how to save unbound textbox value in a table using a command button?
and also how to put a clock in a form. tried all the tutorial from the net.. the time interval property and the ontimer property = vbcode also done but none of them work.. please help me.. thanks
Jan 27 '11 #1
Share this Question
Share on Google+
1 Reply

P: 579
To save the value in an unbound textbox to your table, my preferred approach is to use the (SQL) INSERT INTO statement using VBA.

Let's assume that your table is a student table, called tblStudents, that contains information about the students that are attending school. The primary key of the table would likely be StudentID and let's assume that the value from the textbox you want to enter into the table is StudentLastName. I'm also going to assume that the name of the textbox is txtStudentLastName.

With this in mind, here's an example of how to use the INSERT INTO statement with your command button, which I'm calling CommandButton01:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton01_Click()
  3.      Dim sql As String
  4.      Dim id As Integer
  5.      Dim LName As String
  7.      id = Me.StudentID.Value
  9.      LName = Me.txtStudentLastName.Value
  11.      If Not IsNull(LName) Then
  12.           sql = "INSERT INTO tblStudents ([StudentLastName]) VALUES ('" & LName & "') WHERE tblStudents.StudentID = " & id & ";"
  13.           CurrentDB.Execute (sql)
  14.      Else
  15.           MsgBox "Please enter the student's last name"
  16.           Exit Sub
  17.      End If
  19. End Sub
My sql my not be perfect, but if you search for INSERT INTO you will find plenty of examples. I should also point out that my examples assumes that the primary key for the tblStudents is available on the form so Access knows which specific student to assign the last name to.

As for your second question, I really don't have an answer for that one. I've never put a clock on a form.

Hope this helps,
Jan 27 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.