Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save value, open new form, use value as primary key of new record?

Status
Not open for further replies.

gusbrunston

Programmer
Feb 27, 2001
1,234
US
My brain is swelling, my skill is shrinking, please rescue me!

I would like to use a calculated value in one form to enter a new record in another form.

i.e., Form1Value will become the primary key in a new record in the table that is the data source for Form2.

Form1 is easy.

Form2 is add-new-record-only. I'm not skilled at code, but always seem to work it out (with samples from others' work!)

Thanks for having pity on a veteran! Happy Memorial Day.
:-I

Gus Brunston
An old PICKer
padregus@home.com
 
You can create a function (in the module section) that is going to return the "Form1Value". Than in Form2 assign this function the field that is going to be the primary key (e.g. if the name of this field is, let's say, "ID", than go to the properties window for this field and for the Data/ControlSource write "= PrimaryKeyCode()", where "PrimaryKeyCode()" is the function that you are going to write in the module section).

Now the function:

function PrimaryKeyCode () as string 'If Form1Value is numeric, change "as string" to the appropiate type
'If the form1 is not going to be opened and pointing to Form1Value, than you have write the lines of code between * and * (I considered Table1 is the datasource for Form1).
'*
Dim db As Database
Dim tTable As Recordset

Set db = CurrentDb
Set tTable = db.OpenRecordset("tTable", dbOpenTable)

tTable.MoveFirst
Do While tTable.EOF = False
if 'write your conditions; how you are going to identify which "Form1Value" is the right one' then
PrimaryKeyCode=tTable![Form1Value]
exit
else
acTable.MoveNext
Loop
tTable.close
end function


Hope this helps.
 
oops, i forgot the *. Goes after "tTable.Close"

If you have both opened than just write in the function body PrimaryKeyCode=Forms![Form1]![Form1Value]

 
Dear Simco:
Thank you so very much.
I'm printing out what you gave me as I speak (so to speak), and I'll get to work on it.
:)
Gus Brunston
An old PICKer
padregus@home.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top