Creating a button to create record and open form.

GregoryWest

Registered User.
Local time
Today, 15:36
Joined
Apr 13, 2014
Messages
161
I am sure this has been asked a million times, but I cant seem to find it.

What I need to do is press a button on a form. When the button is pressed, I want to create a new record in a table not already open and populate some fields with data from the form I am looking at. Then call up a new form with the record I just created on it.

Basically it is a work-order entry issue, the user scrolls through to find the correct piece of equipment when they do they click on "Create work-order" a work order is created and populated and the user can then fine tune the new work-order as required.

Any suggestion??
 
Look at

DoCmd.OpenForm

with the argument that opens the form to a new record. Then you can populate it from the first form:

Forms!FormJustOpened.TextboxName = Me.TextboxName
 
Think I have it... Now I am getting a different error.... When I push the buttom I run a Macro that does the following:
Set Warnings On No
OpenQuery NextNewBarcode
RunCode NewPartWorkOrder
OpenForm PartsWorkOrder
SetWarnings On Yes

The VBA for NewPartWorkOrder is in the main module and looks like this:

Public Sub NewPartWorkOrder()
Dim rs_in As DAO.Recordset
Dim rs_out As DAO.Recordset
Dim strSql_in As String
Dim strSql_out As String
Dim OperNumb As Double
Dim PrinterNumb As Double
Dim cust As String

strSql_out = "SELECT * FROM PartWorkOrder;"
Set rs_out = DBEngine(0)(0).OpenRecordset(strSql_out)
strSql_in = "SELECT * FROM ID_Number where DataName = " & Chr(34) & "New BarCode" & Chr(34) & ";"
Set rs_in = DBEngine(0)(0).OpenRecordset(strSql_in)
OrderNumb = Value(Mid(rs_in!NumValue, 2, 9))
rs_in.Close
PrinterNumb = Me.Copier
Set rs_in = Nothing
rs_out.AddNew
rs_out!Order_Number = OrderNumb
rs_out!Date_Required = Date + 1
rs_out!Date_Opened = Date
rs_out!Customer = cust
rs_out!Printer = PrinterNumb
rs_out!Open = True
rs_out!Exported = False
rs_out.Update
rs_out.Close
Set rs_out = Nothing
End Sub


And then I get the error:
The object doesn't contain the Automation object 'NewPartWorkOrder.'

I am at a loss as to what I did wrong....
 
It's in a standard module, not behind a form or report? Also, it probably needs to be a function rather than a sub.
 
Yes it is under the modules section off "All Access Objects" in a container called MainCode. Have all my global VBA code in there. Have another button on the 'Main Menu' which calls a procedure that updates one table. This modulal is different from the first in that it is getting data from an additional table, and from the calling form. Not sure if that is what is causing the problems.
 
Did you switch it to a function?
 
I can try that, how would I do that? Do I just change the header, or do I need to copy past into a function?
 
No, just change "Sub" to "Function" in the first and last lines.
 
Changed to function, got the same error message. Was a good shot....
 
The error comes from the macro? Have you tried including the parentheses in the macro argument?

NewPartWorkOrder()

Can you post the db here?
 
Well added the () to the end.... Got farther.. Now got the error Invalid use of the Me keyword. Highlighting the line: PrinterNumb = Me.Copier

As for posting, would not work, there are external databases involved also.
 
Sorry, didn't notice that in there. Me is a shortcut to refer to the object containing the code (like the form). In a standard module it's not valid, as there is no object. You need the full form reference there if it will always be the same form.
 
Hey Paul, thanks for all your help, everything is working slick right now!! How do I flag this thread as "Solved"?
 
Happy to help! Looks like you found out how to flag it.
 

Users who are viewing this thread

Back
Top Bottom