Discussion Locked

MS Access 97 VBA Question

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Parent Database Name: MEMBERS

Fields:
ID Number
First Name
Last Name
Affiliation
Activity

Child Database Name: DUES

Fields:
ID Number
Dues Year
Amt Due
Amt Paid

Relationship:

Each Member can have several Dues Records ie: 1996, 1997 1998, 1999
Index on ID Numbers UNIQUE for each member

Problem:
On a form which also has a sub form showing the yearly dues amounts, I would like to have a command button. When pressed the button should do the following

1) Open the 2 database files
2) Select members where Activity = MA and Affiliation = AV
3) For each of the selected members, add a NEW Dues record where the year = 2000 and Amt Due = $45.00
4) Refresh the form so that this new record appears in the sub form

3 replies

by bob sellman

Code for on click property of button. Assumes Year column is an integer and dues column is currency. Change the Dim statements to agree with your defined column types. I've put an asterisk at the start of each line in case the answer formatting here doesn't recognize line feeds.:

*Dim tmpYear as Int
*Dim tmpDues as Currency
*Dim strSQL AS String
*tmpYear = 2000
*tmpDues = 45.00
*strSQL = "INSERT INTO DUES (ID Number, Dues Year, Amt Due) SELECT ID Number, " & tmpYear & ", Amt Due = " & tmpDues FROM MEMBERS WHERE Activity = 'MA' AND Affiliation = 'AV';"
*DoCmd.RunSQL strSQL
*me.requery

If tmpYear is a string rather than an integer or long, you must include single quotes around it, so the code would be (... indicates the existing code above before and after):

*...ID Number, '" & tmpYear & "', Amt Due...

Also, the last statement might be me.recalc rather than me.requery.

You can also improve this further by having the user select a year to add, then when the add dues button is clicked you could set the year from the control (call it "cboYear")and the dues from the table. (I'm using asterisks here again. Do not use the asterisks in your code):

*tmpYear = me.cboYear
*tmpDues = DLookup("Amt Due","DUES","Dues Year = " & tmpYear)

Once again, if tmpYear (and Year) are strings, then the second statement would read:

*tmpDues = DLookup("Amt Due","DUES","Dues Year = '" & tmpYear & "'")

This discussion is closed to new replies.