1

I am looking for help on trying to add data into a table / query through text boxes and a button. Currently, there are two buttons that will be hooked up to the text boxes; Search & Add.

Search I have finished already, where it searches a query attached to a table for the input you entered into the text boxes. Simple.

Though now I'd also like to make an add button, where once you put information into the text boxes and click add instead of search, it directly adds that information onto the table and saves it so you can view it at later points in time.

This is code I found online somewhere, but I don't know how to make it pick up the data from the text boxes using it:

Private Sub Command344_Click()

INSERT INTO OrderT (CustomerName,OrderName,OrderDesc,DateOfPurchase,ProjectDueDate,EngineerDueDate,ProjectComplete,CutplanDueDate,MaterialSpecs,CutplanCode,HardwareSpecs,HardwareDueDate,HardwareComplete,PurchaseOrder,PurchaseSupplier);
VALUES (CustomerName,OrderName,OrderDesc,DateOfPurchase,ProjectDueDate,EngineerDueDate,ProjectComplete,CutplanDueDate,MaterialSpecs,CutplanCode,HardwareSpecs,HardwareDueDate,HardwareComplete,PurchaseOrder,PurchaseSupplier);

End Sub

Button Name: Command344

TextBox Names: CustomerName OrderName OrderDesc DateOfPurchase ProjectDueDate EngineerDueDate ProjectComplete CutplanDueDate MaterialSpecs CutplanCode HardwareSpecs HardwareDueDate HardwareComplete PurchaseOrder PurchaseSupplier

The fields in the table have the same names in the exact same order from top -> bottom, left -> right.

The table name is OrderT.

Form name is SearchF

1 Answer 1

5

You can do this either through a query or by picking up the data from your form directly.

To do this in a query, you would put something like this (untested) code behind your button:

    Dim sSQL as String

    Set sSQL = "INSERT INTO OrderT (CustomerName,OrderName,OrderDesc,DateOfPurchase,ProjectDueDate,EngineerDueDate,ProjectComplete,CutplanDueDate,MaterialSpecs,CutplanCode,HardwareSpecs,HardwareDueDate,HardwareComplete,PurchaseOrder,PurchaseSupplier)
    VALUES (" & Me.CustomerName & "," & Me.OrderName & "," & Me.OrderDesc & "," & Me.DateOfPurchase & "," & Me.ProjectDueDate & "," & Me.EngineerDueDate & "," & Me.ProjectComplete & "," & Me.CutplanDueDate & "," & Me.MaterialSpecs & "," & Me.CutplanCode & "," & Me.HardwareSpecs & "," & Me.HardwareDueDate & "," & 
Me.HardwareComplete & "," & PurchaseOrder & "," & Me.PurchaseSupplier & ");"

    DoCmd.RunSQL sSQL

Picking up the data from the data form (my preferred method) would look like this (untested) code:

Dim db as Database
Dim rec as Recordset

Set db = CurrentDB
set rec = db.OpenRecordset ("Select * from OrderT")

rec.AddNew
rec("CustomerName") = Me.CustomerName
rec("OrderName") = Me.OrderName
etc...
rec.Update

Set rec = Nothing
Set db = Nothing
Sign up to request clarification or add additional context in comments.

2 Comments

Second method is better because don't require type manipulations. And first method would not work unless quote strings with ' and reformat dates with # values in VALUES part.
Thank you! It works. I used the code to input it straight to the table; that it releases another problem. I do use a query to show the results of the search onto the form; though for the query to recognize that row in a table every field needs to be filled out; if it doesn't the query acts like it doesn't exists and moves on. How would I make it so it can take all data from the table and input it onto the subform, and if a field is blank it just leaves it as null on the subform?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.