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: - Function ReadCourseContent()
-
Dim db As DAO.Database
-
Dim rst As DAO.Recordset
-
-
Set db = CurrentDb()
-
Set rst = CurrentDb.OpenRecordset("CourseContent", dbOpenSnapshot)
-
-
rst.FindFirst "[CourseCode] = 'BW310_74'"
-
-
Do While Not rst.NoMatch
-
Debug.Print rst!UnitNr, rst!Unit, rst!Content
-
rst.FindNext "[CourseCode] = 'BW310_74'"
-
Loop
-
-
-
rst.Close
-
-
Set rst = Nothing
-
db.Close
-
Set db = Nothing
-
-
SendKeys "^g"
-
-
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: - Function ReadCourseContent(strCourseCode)
-
Dim db As DAO.Database
-
Dim rst As DAO.Recordset
-
-
Set db = CurrentDb()
-
Set rst = CurrentDb.OpenRecordset("CourseContent", dbOpenSnapshot)
-
-
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
13 16980 ADezii 8,834
Recognized Expert Expert - Function Definition:
- Public Function ReadCourseContent(strCourseCode As String)
-
Dim db As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim strSQL As String
-
-
strSQL = "Select * From CourseContent Where [CourseCode] = '" & strCourseCode & "';"
-
-
Set db = CurrentDb()
-
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
-
-
With rst
-
Do While Not .EOF
-
Debug.Print !UnitNr, !Unit, !Content
-
rst.MoveNext
-
Loop
-
End With
-
-
rst.Close
-
-
Set rst = Nothing
-
Set db = Nothing
-
-
DoCmd.RunCommand acCmdDebugWindow
-
End Function
- Sample Usage:
- Call ReadCourseContent("BW310_74")
Absolutely marvellous - thanks!!!
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!
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. - Public Function ReadCourseContent(strCourseCode As String)
-
Dim db As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim strSQL As String
-
Dim cbo As ComboBox
-
-
strSQL = "Select * From CourseContent Where [CourseCode] = '" & strCourseCode & "';"
-
-
Set db = CurrentDb()
-
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
-
-
Set cbo = Forms!frmTest!cboCourseInfo
-
-
'Define the characteristics of the Combo Box
-
cbo.RowSourceType = "Value List" 'Critical
-
cbo.ColumnCount = 3
-
cbo.BoundColumn = 1 'Bind the 1st Column
-
cbo.ColumnWidths = "0 in;1 in;1 in" 'Hide the 1st Column
-
-
With rst
-
Do While Not .EOF
-
cbo.AddItem !UnitNr & "," & !Unit & "," & !Content
-
rst.MoveNext
-
Loop
-
End With
-
-
rst.Close
-
-
Set rst = Nothing
-
Set db = Nothing
-
End Function
NeoPa 32,572
Recognized Expert Moderator MVP
Remember ComboBoxes don't support multiple selections. For that you would need a properly specified ListBox.
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...
ADezii 8,834
Recognized Expert Expert
How is this SubForm related to the Main Form?
Apologies for the delay - a couple of manic weeks at work... The subform was simply created separately and dragged on to the main form...
ADezii 8,834
Recognized Expert Expert
What is? - The Name of the Main Form?
- The Name of the Sub-Form?
- The Record Source of the Sub-Form?
- You would not be populating the Sub-Form itself, but the Record Source of the Sub-Form, then Requery it.
- The Sub-Form is totally independent from the Main Form, correct?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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
|
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
|
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
-----------------------------------------------------------------------
|
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"
| |
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;
|
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)
|
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...
|
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...
|
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:
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |