473,763 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass a variable to FindFirst in DAO Recordset?

7 New Member
Hi,

I am new to using VBA, and have been working with DAO Recordsets. I'm working on a little problem, and think that DAO Recordsets are the solution. I've been playing around with them to try and get it working, and have the following code, which works perfectly:
Expand|Select|Wrap|Line Numbers
  1. Function ReadCourseContent()
  2.      Dim db As DAO.Database
  3.      Dim rst As DAO.Recordset
  4.  
  5.      Set db = CurrentDb()
  6.      Set rst = CurrentDb.OpenRecordset("CourseContent", dbOpenSnapshot)
  7.  
  8.      rst.FindFirst "[CourseCode] = 'BW310_74'"
  9.  
  10.      Do While Not rst.NoMatch
  11.           Debug.Print rst!UnitNr, rst!Unit, rst!Content
  12.           rst.FindNext "[CourseCode] = 'BW310_74'"
  13.      Loop
  14.  
  15.  
  16.      rst.Close
  17.  
  18.      Set rst = Nothing
  19.      db.Close
  20.      Set db = Nothing
  21.  
  22.      SendKeys "^g"
  23.  
  24. End Function
What I want to do now, is to pass in a variable value, rather than coding the CourseCode. I thought (in my ignorance) that declaring something like Function ReadCourseConte nt(strCourseCod e) would allow me to pass a course code into the function, and that I could use this in place of the string 'BW310_74' - something like this:
Expand|Select|Wrap|Line Numbers
  1. Function ReadCourseContent(strCourseCode)
  2.      Dim db As DAO.Database
  3.      Dim rst As DAO.Recordset
  4.  
  5.      Set db = CurrentDb()
  6.      Set rst = CurrentDb.OpenRecordset("CourseContent", dbOpenSnapshot)
  7.  
  8.      rst.FindFirst "[CourseCode] = " & strCourseCode
However, this fails, telling me I have a missing operator in the expression, and in VBE, the last line of code above is highlighted yellow, telling me strCourseCode=E mpty.

Is there a way to pass a variable into the FindFirst and FindNext methods of the DAO Recordset?

Cheers
Jan 7 '10 #1
13 16980
ADezii
8,834 Recognized Expert Expert
  1. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function ReadCourseContent(strCourseCode As String)
    2. Dim db As DAO.Database
    3. Dim rst As DAO.Recordset
    4. Dim strSQL As String
    5.  
    6. strSQL = "Select * From CourseContent Where [CourseCode] = '" & strCourseCode & "';"
    7.  
    8. Set db = CurrentDb()
    9. Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
    10.  
    11. With rst
    12.   Do While Not .EOF
    13.     Debug.Print !UnitNr, !Unit, !Content
    14.       rst.MoveNext
    15.   Loop
    16. End With
    17.  
    18. rst.Close
    19.  
    20. Set rst = Nothing
    21. Set db = Nothing
    22.  
    23. DoCmd.RunCommand acCmdDebugWindow
    24. End Function
  2. Sample Usage:
    Expand|Select|Wrap|Line Numbers
    1. Call ReadCourseContent("BW310_74")
Jan 7 '10 #2
ChrisD76
7 New Member
Absolutely marvellous - thanks!!!
Jan 7 '10 #3
ChrisD76
7 New Member
Thanks again for your assistance on this. I have now extended the code to attempt the next step, which is to write the content of the recordset into an array, and have run into a problem. I have now got two questions - should I be using an array at all? If so, what have I done wrong?

My intention here is to have a form with a multiselection combo box. Based on the CourseCode (or CourseCodes) selected, I want Access to read the CourseContent table, and write the list of course content into a form. The form will include a tick box next to each unit of the course, allowing the user to select which unit they want to include, and then this will to output to a report.

The code I posted originally, and which is now corrected, allows for a single CourseCode to be read into a recordset. What I want to do now is to prepare this data to be written to a form - can this be done straight from the recordset, or do I need to write the data into an array first? Eventually I need to go back and adjust the code to accept multiple CourseCodes.

This seems really straight forward in my head, but translating it into VBA has been challenging - I guess I am missing something basic. Before this week I had never used VBA, so please forgive my ignorance.

So my question then is should I be using an array at this point, or can I write directly to a form from the recordset? If I should use an array, then I will post the code for opinion on what I'm doing wrong...

Many thanks!
Jan 7 '10 #4
ADezii
8,834 Recognized Expert Expert
The values can be derived directly from the Recordset. The following code will populate the cboCourseInfo Combo Box on frmTest (must be Open) directly from the Do...Loop navigating the Recordset. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Public Function ReadCourseContent(strCourseCode As String)
  2. Dim db As DAO.Database
  3. Dim rst As DAO.Recordset
  4. Dim strSQL As String
  5. Dim cbo As ComboBox
  6.  
  7. strSQL = "Select * From CourseContent Where [CourseCode] = '" & strCourseCode & "';"
  8.  
  9. Set db = CurrentDb()
  10. Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
  11.  
  12. Set cbo = Forms!frmTest!cboCourseInfo
  13.  
  14. 'Define the characteristics of the Combo Box
  15. cbo.RowSourceType = "Value List"    'Critical
  16. cbo.ColumnCount = 3
  17. cbo.BoundColumn = 1     'Bind the 1st Column
  18. cbo.ColumnWidths = "0 in;1 in;1 in"     'Hide the 1st Column
  19.  
  20. With rst
  21.   Do While Not .EOF
  22.     cbo.AddItem !UnitNr & "," & !Unit & "," & !Content
  23.       rst.MoveNext
  24.   Loop
  25. End With
  26.  
  27. rst.Close
  28.  
  29. Set rst = Nothing
  30. Set db = Nothing
  31. End Function
Jan 7 '10 #5
NeoPa
32,572 Recognized Expert Moderator MVP
Remember ComboBoxes don't support multiple selections. For that you would need a properly specified ListBox.
Jan 7 '10 #6
ChrisD76
7 New Member
Thanks guys for your help. I was actually hoping to write the recordset to a form, something like the attached. I cannot figure out how to get the recordset to populate the subform. Once this data is in the subform, the idea is that the user can simply tick the box next to the units they want to include and then save this data into a separate table, and at the same time generate a report to be printed. And as I mentioned previously, I need to be able to include multiple units from multiple course codes...
Attached Images
File Type: jpg CSR_Form.jpg (9.0 KB, 717 views)
Jan 8 '10 #7
ADezii
8,834 Recognized Expert Expert
How is this SubForm related to the Main Form?
Jan 8 '10 #8
ChrisD76
7 New Member
Apologies for the delay - a couple of manic weeks at work... The subform was simply created separately and dragged on to the main form...
Jan 26 '10 #9
ADezii
8,834 Recognized Expert Expert
What is?
  1. The Name of the Main Form?
  2. The Name of the Sub-Form?
  3. The Record Source of the Sub-Form?
  4. You would not be populating the Sub-Form itself, but the Record Source of the Sub-Form, then Requery it.
  5. The Sub-Form is totally independent from the Main Form, correct?
Jan 26 '10 #10

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

Similar topics

5
7083
by: GB | last post by:
Hello: In my MS Access project I have two objects - my form and my report. I need to pass variable value from Command_click procedure of my form to Report_open procedure of my report. How can I do that? I tried to declare this variable as Public in my form, but it does not work - when I reference to this variable in my report, it does not pass the value.
1
3148
by: Ben Kial | last post by:
I'd like to write a wrapper function "mysprintf(char *buffer, char *format, ....)" which calls sprintf(). My question is how can I pass variable argument in mysprintf() to sprintf(). Thanks in advance... Ben
8
6579
by: darrel | last post by:
I'm still trying to fully understand how best to pass variables between pages/usercontrols/each other. On a current site I've done, I've had one userControl do the logic and set the variable, and then I had other usercontrols simply read this by traversing the class structure: siteClass.userControlClass.specficVariable. That worked fine. The new site I'm working on is a bit different, as I'm using multiple
1
2424
by: dollar | last post by:
I want to ask what is the best way to pass variable between asp.net we pages, and user controls , any examples - dolla ----------------------------------------------------------------------- Posted via http://www.codecomments.co -----------------------------------------------------------------------
2
19794
by: phil | last post by:
Hi, I want to put a recordset in a gridview but i don't know how to pass the value of the variable in the 'where' statement. The value of the variable is set in the code-behind. i added a tag <selectParameters> but i don't know which elements are needed and how... This is my aspx file: <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydb.mdb"
1
7746
by: drec | last post by:
I need to pass a variable from script1.php to script2.php. The problem is that these scripts use frames and therefore I am able to pass the variable through the URL. It also does not use forms to set this variable, so i cannot pass it this way either. So to simplify I have : script1.php <?php $var1 = value;
1
4062
by: vijaynarang | last post by:
strCmdLine = " " + " -o restore -url " + sSitePath + "/sites/" + sToplevelSiteName + " -filename " + sFileName+ " -overwrite "; process1 = System.Diagnostics.Process.Start(Application.StartupPath + @"\" + "STSADM.EXE", strCmdLine); process1.WaitForExit(); process1.Close(); I want double qoute around sfilename Please tell me how to pass variable containing ' " " (double qoute)
5
5748
by: techbuddha | last post by:
Hi new to the forum and visual basic. I am attempting to fix a migration of excel to access. The excel sheets where simple copied as is into access. for example one table lists the academic history from elementary to phd and on to post doctoral work alll on the same row. I want to break that up to have each level of education as a seperate record and then relate that back to the person. i can pull the data into a recordset I can...
12
6926
by: simon2x1 | last post by:
i have a page which is home.php on that page i have a CSS tab which is tab1 and tab2 on that page in both tabs i have a link called next that pass variable to the next page (<a href='home.php?page=$lastpage'>NEXT</a>).if i click on next in the tab1 on home page(home.php) it will pass the variable and bring me to the home page and tab1 if i click on the next in tab2 it will also bring me to home page and take me to tab1 back.i want it to bring...
2
2896
by: peachdot | last post by:
hi, The MainForm will have 2 buttons: 1.) Button A : User click button A, hide Mainform then go to form1. User enter data in the textbox.Click finish button,form1 close then go back to MainForm. 2.) Button B : User Click Button B,hide Mainform then go to form2. Click a button & some mathematical operations will be done using all parameters that have been entered in form1. My method of passing variable is:
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9828
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.