473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help Creating a loop

cori25
83
I have 4 database's that I need to enter and run a macro in. The macro in each db is named the same. I am having difficulty creating a loop to go into one db run the macro exit and then go into another db run the macro exit, etc...

Any help appreciated


Function Delete()

Dim DbPath$
Dim intMacro As Integer

For intMacro = 0 To 4

Select Case intMacro

Case 0 'Delete Glenn
DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Gle nn Laudenslager\OverTime(Glenn).mdb"

Dim db As New Access.Application
db.OpenCurrentDatabase (DbPath)
db.Visible = True
db.DoCmd.RunMacro ("DeleteInput")
db.Quit

Next intMacro

Case 1 'Delete Heather
DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Hea ther Foster\OverTime(Heather).mdb"

db.OpenCurrentDatabase (DbPath)
db.Visible = True
db.DoCmd.RunMacro ("DeleteInput")
db.Quit

Next intMacro
Feb 7 '08 #1
7 1939
ADezii
8,834 Expert 8TB
I have 4 database's that I need to enter and run a macro in. The macro in each db is named the same. I am having difficulty creating a loop to go into one db run the macro exit and then go into another db run the macro exit, etc...

Any help appreciated


Function Delete()

Dim DbPath$
Dim intMacro As Integer

For intMacro = 0 To 4

Select Case intMacro

Case 0 'Delete Glenn
DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Gle nn Laudenslager\OverTime(Glenn).mdb"

Dim db As New Access.Application
db.OpenCurrentDatabase (DbPath)
db.Visible = True
db.DoCmd.RunMacro ("DeleteInput")
db.Quit

Next intMacro

Case 1 'Delete Heather
DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Hea ther Foster\OverTime(Heather).mdb"

db.OpenCurrentDatabase (DbPath)
db.Visible = True
db.DoCmd.RunMacro ("DeleteInput")
db.Quit

Next intMacro
You have 4 Databases, but the For...Next is being executed 5 times. Change to:
Expand|Select|Wrap|Line Numbers
  1. For intMacro = 1 To 4
Feb 9 '08 #2
Minion
108 Expert 100+
You have 4 Databases, but the For...Next is being executed 5 times. Change to:
Expand|Select|Wrap|Line Numbers
  1. For intMacro = 1 To 4
Good catch. Also make sure that when you change your loop from 1 to 4 (or 0 to 3) that you make sure to adjust your Select Case to match.

- Minion -
Feb 9 '08 #3
ADezii
8,834 Expert 8TB
I have 4 database's that I need to enter and run a macro in. The macro in each db is named the same. I am having difficulty creating a loop to go into one db run the macro exit and then go into another db run the macro exit, etc...

Any help appreciated


Function Delete()

Dim DbPath$
Dim intMacro As Integer

For intMacro = 0 To 4

Select Case intMacro

Case 0 'Delete Glenn
DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Gle nn Laudenslager\OverTime(Glenn).mdb"

Dim db As New Access.Application
db.OpenCurrentDatabase (DbPath)
db.Visible = True
db.DoCmd.RunMacro ("DeleteInput")
db.Quit

Next intMacro

Case 1 'Delete Heather
DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Hea ther Foster\OverTime(Heather).mdb"

db.OpenCurrentDatabase (DbPath)
db.Visible = True
db.DoCmd.RunMacro ("DeleteInput")
db.Quit

Next intMacro
I made a couple of changes, try this code and see what happens:
Expand|Select|Wrap|Line Numbers
  1. Function Delete()
  2. Dim DbPath As String, db As Access.Application
  3. Dim intMacro As Integer
  4.  
  5. For intMacro = 1 To 4
  6.   Select Case intMacro
  7.     Case 1 'Delete Glenn
  8.       DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Glen n Laudenslager\OverTime(Glenn).mdb"
  9.       Set db = CreateObject("Access.Application")
  10.         db.OpenCurrentDatabase (DbPath)
  11.         db.Visible = True   'why?
  12.         db.DoCmd.RunMacro ("DeleteInput")
  13.           db.Quit
  14.     Case 2 'Delete Heather
  15.       DbPath = "\\Stratford1\COMMON1\SHARED\DISPATCH\Overtime\Heat her Foster\OverTime(Heather).mdb"
  16.       Set db = CreateObject("Access.Application")
  17.         db.OpenCurrentDatabase (DbPath)
  18.         db.Visible = True   'why?
  19.         db.DoCmd.RunMacro ("DeleteInput")
  20.           db.Quit
  21.   Case 3    'additional code needed here
  22.   Case 4    'additional code needed here
  23.   Case Else
  24.     'drop thru code, won't happen, but
  25. Next intMacro
  26. End Function
Feb 9 '08 #4
Jim Doherty
897 Expert 512MB
If you're doing what I think your are doing it occurs to me that you have a consistency of folder path and a naming convention for your mdb files that basically provides one overtime mdb per person.

Have a look at this merely as 'food for thought' in that if you get any more people added to the overtime path folder having their own mdb file then simply adding their name to the array and resizing it will provide for examining future mdb files and running the macro in each one of their mdb files as well.

You can enhance this routine of course , but no point unless you have a variable number of people etc etc...error handling needs addressing too.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function fn_DeleteInputs()
  3. Dim db As New Access.Application
  4.     Dim sharedpath As String, filename As String
  5.     Dim strperson(1) As String
  6.     'define the various elements of the zero based array
  7.     strperson(0) = "Glenn Laudenslager"
  8.     strperson(1) = "Heather Foster"
  9.     'if and only if the folder location is fixed and the mdb file naming convention is consistent
  10.     'then define a path commensurate with the person name and pin the name of the
  11.     'mdb to it BUT ONLY if we can parse first name from surname and concatenate the file extension
  12.     'Loop through the elements of the array during 'one' instance of the Access
  13.     'application (resource preservation)and run the macro in each database.
  14.     'Close off and quit after loop finishes
  15.     For Each element In strperson
  16.         sharedpath = "\\Stratford1\Common1\Shared\Dispatch\Overtime\" & element & "\"
  17.         filename = "Overtime(" & Trim(left(element, InStr(element, " "))) & ").mdb"
  18.         Debug.Print sharedpath & filename
  19.         db.OpenCurrentDatabase (sharedpath & filename)
  20.         db.DoCmd.RunMacro ("DeleteInput")
  21.         db.CloseCurrentDatabase
  22.     Next
  23.     db.Quit
  24. End Function
  25.  
Regards

Jim :)
Feb 10 '08 #5
ADezii
8,834 Expert 8TB
If you're doing what I think your are doing it occurs to me that you have a consistency of folder path and a naming convention for your mdb files that basically provides one overtime mdb per person.

Have a look at this merely as 'food for thought' in that if you get any more people added to the overtime path folder having their own mdb file then simply adding their name to the array and resizing it will provide for examining future mdb files and running the macro in each one of their mdb files as well.

You can enhance this routine of course , but no point unless you have a variable number of people etc etc...error handling needs addressing too.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function fn_DeleteInputs()
  3. Dim db As New Access.Application
  4.     Dim sharedpath As String, filename As String
  5.     Dim strperson(1) As String
  6.     'define the various elements of the zero based array
  7.     strperson(0) = "Glenn Laudenslager"
  8.     strperson(1) = "Heather Foster"
  9.     'if and only if the folder location is fixed and the mdb file naming convention is consistent
  10.     'then define a path commensurate with the person name and pin the name of the
  11.     'mdb to it BUT ONLY if we can parse first name from surname and concatenate the file extension
  12.     'Loop through the elements of the array during 'one' instance of the Access
  13.     'application (resource preservation)and run the macro in each database.
  14.     'Close off and quit after loop finishes
  15.     For Each element In strperson
  16.         sharedpath = "\\Stratford1\Common1\Shared\Dispatch\Overtime\" & element & "\"
  17.         filename = "Overtime(" & Trim(left(element, InStr(element, " "))) & ").mdb"
  18.         Debug.Print sharedpath & filename
  19.         db.OpenCurrentDatabase (sharedpath & filename)
  20.         db.DoCmd.RunMacro ("DeleteInput")
  21.         db.CloseCurrentDatabase
  22.     Next
  23.     db.Quit
  24. End Function
  25.  
Regards

Jim :)
Nice catch, Jim, and more efficient too. Well, another one that went right over-my-head! (LOL).
Feb 10 '08 #6
cori25
83
Thanks for the advise, will try and see how it goes!
Nice catch, Jim, and more efficient too. Well, another one that went right over-my-head! (LOL).
Feb 11 '08 #7
cori25
83
Thanks so much! The loop ran perfectly!

Nice catch, Jim, and more efficient too. Well, another one that went right over-my-head! (LOL).
Feb 11 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
0
by: Sarah | last post by:
Hi, I have an Access database of about 1000 records. I am trying to use asp to loop through the record set one at a time, creating the html code for a web page. I would like to save the actual...
1
by: steven | last post by:
I am creating some financial reports in Access 2000, and I am having problem that I can't resolve. The report sorts information first by vendor, then by budget line number. It generates the...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
3
by: mslyman | last post by:
Hi, I could do with some help. I have this XML. <region> <region_code>567</region_code> <store> <store_code>345</store_code> <dept> <dept_code>32</dept_code> </dept>
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
14
by: bcap | last post by:
Hello, I really would apprciate help! =) What I want to do is be able to change the status of mulitple records using a drop down and a checkbox. I have a drop down called "ChangeStatus"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.