Connecting Tech Pros Worldwide Help | Site Map

open form to specific record or create new record

Newbie
 
Join Date: Nov 2006
Posts: 2
#1: Nov 13 '06
I have a database that I am using to store student data. There are two tables and two forms to display the data:

Tables
tbStudent
tbRE

Forms
fmStudent
fmRE

tbStudent stores basic student data, while tbRE stores specific academic data for some, but not all, students. The two tables are linked through the student ID number(stStuID), which is the key in tbStudent.

I would like to create a command button in fmStudent that opens fmRE, but to the record of the student currently displayed in fmStudent. Aside from opening fmRE to a specific record, I am also curious about what happens if a student does not have a record in fmRE. Could I have the command open fmRE to a blank record if the student doesn't have a record, so the user can enter the data to create one?

I have not used VBA very much so I am new to this level of design. I've seen some responses to this type of question, but I'm not sure how to get started with the coding.

I am using Access 2003 on Windows XP.

Thanks!
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#2: Nov 13 '06

re: open form to specific record or create new record


The easiest way to do this is to use a subform on your form that is the fmRE. Bind it using the stStuID field. This will display the records for students who have them and a blank record where there is no record available.

If you try to open another form you will have problems with the ones that haven't any records. For future reference the code would be:

DoCmd.OpenForm "fmRE", , , "[stStuID]=" & Me.stStuID

Quote:

Originally Posted by cmp80

I have a database that I am using to store student data. There are two tables and two forms to display the data:

Tables
tbStudent
tbRE

Forms
fmStudent
fmRE

tbStudent stores basic student data, while tbRE stores specific academic data for some, but not all, students. The two tables are linked through the student ID number(stStuID), which is the key in tbStudent.

I would like to create a command button in fmStudent that opens fmRE, but to the record of the student currently displayed in fmStudent. Aside from opening fmRE to a specific record, I am also curious about what happens if a student does not have a record in fmRE. Could I have the command open fmRE to a blank record if the student doesn't have a record, so the user can enter the data to create one?

I have not used VBA very much so I am new to this level of design. I've seen some responses to this type of question, but I'm not sure how to get started with the coding.

I am using Access 2003 on Windows XP.

Thanks!

NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,714
#3: Nov 13 '06

re: open form to specific record or create new record


Quote:

Originally Posted by mmccarthy

DoCmd.OpenForm "fmRE", , , "[stStuID]=" & Me.stStuID

Or if, as I suspect, the st of stStuID stands for string, then you must add quotes around the variable in your code thus :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "fmRE", , , "[stStuID]='" & Me.stStuID & "'"
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#4: Nov 13 '06

re: open form to specific record or create new record


Quote:

Originally Posted by NeoPa

Or if, as I suspect, the st of stStuID stands for string, then you must add quotes around the variable in your code thus :

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "fmRE", , , "[stStuID]='" & Me.stStuID & "'"

Good call.

I just assumed that ID would be a number. I have to stop doing that.



Mary
Reply