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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How should I write this VBA code? 1

Status
Not open for further replies.

jane30

Programmer
Nov 14, 2000
92
US
Hi, All!

I have a VBA code that run various macros based on the field values[FIELD1] in TBL1:

Sub chkappend()
Dim db As Database
Dim rst As Recordset
Dim intF1 As Integer

Set db = CurrentDb
Set rst = db.OpenRecordset("TBL1")
intF1 = rst.RecordCount

If intF1 = 0 Then
MsgBox ("No need to update.")
Else

Select Case rst![FIELD1]
Case Is = "AA"
DoCmd.RunMacro ("Mcr1.AA")

Case Is = "BB"
DoCmd.RunMacro ("Mcr1.BB")

End Select

End If

When all the criteria are met, only the first case statement was executed, i.e. only Mcr1.AA was run, and the Mcr1.BB was ignored even [Field1] has a value = "BB".

Basically, I need to read throught TBL1. If [FIELD1]="AA", run Mcr1.AA, if = "BB", run Mcr1.BB. And this VBA code is part of my AutoExec job. So, it won't tight with with any controls in the form or report.

What did I do wrong? Thank you for your time and help.
 
This will need a little polishing, but here's the idea. This routine uses your recordset based on TBL1.


Sub chkappend()
Dim strMyMacro
Dim db As Database
Dim rst As Recordset
Dim intF1 As Integer

Set db = CurrentDb
Set rst = db.OpenRecordset("TBL1")
rst.movelast
rst.MoveFirst
intF1 = rst.RecordCount

If intF1 = 0 Then
MsgBox ("No need to update.")
Else


Do While not rst.EOF
strMacro = rst.fields("Your Field")
DoCmd.RunMacro strMyMacro
rst.movenext
Loop

End If Tyrone Lumley
augerinn@gte.net
 
Try these changes;

Set db = CurrentDb
Set rst = db.OpenRecordset("TBL1")
With rst
If .RecordCount= 0 Then
MsgBox ("No need to update.")
Exit Sub
Else
.MoveLast
.MoveFirst
Do Until .EOF
Select Case ![FIELD1]
Case Is = "AA"
DoCmd.RunMacro ("Mcr1.AA")
Case Is = "BB"
DoCmd.RunMacro ("Mcr1.BB")
End Select
.MoveNext
Loop
End If
End With

You need to 'populate' the recordset then step through each record and check the value of ![FIELD1].
I like to use the With statement, It can make the code easier to read.
 
Thank you very much! You're right, I should loop through the whole table to continue the condition checking.

It works perfectly now.
 
I'm trying to switch from FoxPro to Access and I'm having problems learning to code. What advise can you give a X-base programmer to become familiar with Access and VBA coding???

Thanks for your help
 
1. Help in Access. In the VBE (Code window) hit F2 and you can see all of the objects. When you're writing code, double click a word (I.E. Database) and hit F1.

2. Microsoft Knowledge Base

3. MSDN

4. Sybex book. ISBN 0-7821-2370-8

Tyrone Lumley
augerinn@gte.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top