MSAccessRookie
AWF VIP
- Local time
- Today, 16:36
- Joined
- May 2, 2008
- Messages
- 3,428
I have the need to have an Excel (2003) Spreadsheet open my Access (2003) database to locate data to display on the Spreadsheet. Below is a Function that was crafted from bits and pieces of several examples that I have found on the internet, and is intended to do just that.
The Function successfully completes Steps 1-3, and hangs up when it attempts to perform Step 4. Am I approaching this incorrectly?
The Function successfully completes Steps 1-3, and hangs up when it attempts to perform Step 4. Am I approaching this incorrectly?
Code:
[FONT=Courier New][FONT=Courier New][COLOR=gray][B]Option Explicit[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=black][B]----------------------------------------------------------------------[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B]Function GetIngredientByLot(LotNumber As String)[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] 'Step 1: Declare the necessary variables[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Dim dbInfo As DAO.Database[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Dim rst As DAO.Recordset[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Dim strSQL As String[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] 'Step 2: Identify the database and query[/B][/COLOR][/FONT]
[FONT=Courier New][FONT=Courier New][COLOR=blue][B] 'THIS OPENS THE CORRECT DATABASE[/B][/COLOR][/FONT]
[B][COLOR=green] Set dbInfo = OpenDatabase("C:\Documents and Settings\user\Desktop\RDIFrontEnd.mdb")[/COLOR][/B][/FONT]
[FONT=Courier New][COLOR=green][B] 'Step 3: Build the SQL String which will become the recordset[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] strSQL = "SELECT tblIngredients.Ingredient FROM tblRDINGRED " & _[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] "INNER JOIN tblIngredients ON tblRDINGRED.IngredientID = tblIngredients.IngredientID " & _[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] "WHERE tblRDINGRED.RD_Lot = '" & LotNumber & "'"[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=blue][B] 'THIS CREATES THE CORRECT SQL QUERY[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Debug.Print strSQL[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] 'Stop[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] 'Step 4: Open a snapshot (view only) type recordset based on strSQL above[/B][/COLOR][/FONT]
[FONT=Courier New][FONT=Courier New][COLOR=blue][B] 'THE FUNCTION HANGS UP AT THIS POINT [/B][/COLOR][/FONT]
[/FONT][FONT=Courier New][COLOR=red][B] Set rst = dbInfo.OpenRecordset(strSQL, dbOpenSnapshot)[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Debug.Print LotNumber[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Debug.Print rst("Ingredient")[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] 'Stop[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] 'Step 5: Return the ingredient name from the SQL call[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] GetIngredientByLot = rst("Ingredient")[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] 'Step 5: Clear the necessary variables[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Set dbInfo = Nothing[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] Set rst = Nothing[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B] strSQL = ""[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=green][B]End Function[/B][/COLOR][/FONT]
[/FONT]