OK, I've searched all over and used the help on the VBA editor. Helped a little but I'm not getting close enough to see where I'm going wrong.
I need a public variable that I will be able to use on different forms as part of the join property in a SQL statement. I think it would easier to make it a public so I don't have to create the variable in every button it might be used in. Here is what I've come up with so far. While I get no errors when I complie or run the functions its not giving any data in the variable either so while it runs its not really doing anything helpful.
Saved Module -
Public Function GetCS_NO(gCs_No) As String
-
' Create global variable for cs_no join from the Ref_data table join_cs_no
-
Dim rs1 As New ADODB.Recordset
-
Dim strConn As String ' stores the connection string
-
'Dim gCs_No As String
-
-
strConn = CurrentProject.Connection
-
' set the srcZoneTbl variable to name of the table given
-
Set rs1 = New ADODB.Recordset
-
rs1.Open "SELECT join_cs_no FROM Ref_Data", strConn, adOpenDynamic
-
-
If rs1.RecordCount <> 0 Then
-
rs1.MoveFirst
-
gCs_No = rs1("join_cs_no").Value
-
Else
-
Debug.Print rs1("join_cs_no").Value
-
End If
-
-
End Function
-
Just to test if my variable is being pulled correctly I added this to my form that opens with the DB. -
Private Sub Form_Open(Cancel As Integer)
-
Dim strCs_No As String
-
-
strCs_No = GetCS_NO(gCs_No)
-
MsgBox strCs_No
-
End Sub
-
I get the message box, but it is empty.
I want to be able to use the variable strCs_No later in SQL statements, so I'm just trying at this poing to get the public variable to actually pass the data.
Anyone have any ideas? I'm not having any luck so far.
Thanks,
Will
10 2640
If you want the variable to be global, declare it at the top of a Module, rather than in a Function.
If you want to call a public function that returns a value, use - strCs_No = Get_CS_No()
-
-
Public Function Get_CS_No() As String
-
Get_CS_No = "return this string"
-
End Function
I used the code listed about and add the
like this -
strCs_No = GetCS_NO()
-
-
Public Function GetCS_NO() As String
-
' Create global variable for cs_no join from the Ref_data table join_cs_no
-
Dim rs1 As New ADODB.Recordset
-
Dim strConn As String ' stores the connection string
-
'Dim gCs_No As String
-
-
strConn = CurrentProject.Connection
-
' set the srcCS_NO variable to name of the join property given
-
Set rs1 = New ADODB.Recordset
-
rs1.Open "SELECT join_cs_no FROM Ref_Data", strConn, adOpenDynamic
-
-
If rs1.RecordCount <> 0 Then
-
rs1.MoveFirst
-
GetCS_NO = rs1("join_cs_no").Value
-
Else
-
Debug.Print rs1("join_cs_no").Value
-
End If
-
-
End Function
-
And I get an error stating: Compile error: Invalid outside procedure
Ok, i got it to work. But can someone look over this and see if it actually done correctly. Or is there a better way of doing this?
Module code -
Public Function GetCS_NO() As String
-
' Create global variable for cs_no join from the Ref_data table join_cs_no
-
Dim rs1 As New ADODB.Recordset
-
Dim strConn As String ' stores the connection string
-
'Dim gCs_No As String
-
-
strConn = CurrentProject.Connection
-
' set the srcCS_NO variable to name of the join property given
-
Set rs1 = New ADODB.Recordset
-
rs1.Open "SELECT join_cs_no FROM Ref_Data", strConn, adOpenDynamic
-
-
If rs1.RecordCount <> 0 Then
-
rs1.MoveFirst
-
GetCS_NO = rs1("join_cs_no").Value
-
Else
-
Debug.Print rs1("join_cs_no").Value
-
End If
-
-
End Function
-
first form -
Public gCs_No As String
-
Private Sub Form_Open(Cancel As Integer)
-
Dim strCs_No As String
-
-
strCs_No = GetCS_NO()
-
MsgBox strCs_No
-
End Sub
-
If you want a single value from a table, it's much easier and clearer to use DLookup rather than a recordset.
So if I were to use DLookup in this case it would look like this? -
-
DLookup(join_cs_no, Ref_Data)
-
-
In this case?
Close. The arguments should be string expressions, or the compiler will try to resolve them as variables. - DLookup("join_cs_no", "Ref_Data")
then just set the dlookup to some variable i need I would assume.
That does seem a little easier... LOL!
Thanks for the help ChipR!
NeoPa 32,556
Expert Mod 16PB
Do you have this data stored in your [Ref_Data] table Will?
If so, this is probably a better approach than a global variable, which would be declared as : - Public {name} As {Type} = {Value}
in a standard (not class) module, and at the start before any executable code.
Is there only the one record in your [Ref_Data] table?
Yes, by Ref_Data table has just 1 record and several fields. I just wanted the 1 record so I didn't have to put the results in an array and split all that out and what not. The only question I would have about that suggestion is how I would make it pull the {Value}
I have tried this -
Public strCs_No As String = GetCS_NO()
-
But I get an error when I push enter to go to the next line. The error I get is "Compile error: Expected: end of statement"
NeoPa 32,556
Expert Mod 16PB
Sorry. Being a muppet again. That should have been :
I was confusing with the Const declaration : - Public Const {Name} As {Type} = {Expression}
NB. I was actually suggesting that Chip's idea of referring to it using DLookup() seems sound. Your single record setup is spot on.
PS. Note that {Expression} cannot include a function reference. Function calls within a declaration cannot work. This is not an executable stage of the code.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Scott Lyon |
last post by:
I'm maintaining (read: I didn't write it, nor do I have the time to spend to
rewrite it) an application that is suddenly giving me grief.
The reason I say suddenly, is because we're in the...
|
by: Mark |
last post by:
Hi,
Im trying to validate a form, all the validating works apart from one field.
This particular field must consist of the first 2 characters as letters, &
the following 5 as numbers. And if it...
|
by: cloverme |
last post by:
Hi,
I need help populating a listbox from a database on a webform.
I created a an access database with a table, fields and data. Then I
created a WebForm in vb.net and added a DropDownList...
|
by: coleenholley |
last post by:
Hi All :-)
I need to populate a table I created as a web form. Are there any links to show me how to do this? I CANNOT use a dtagrid for this, the table has to be laid out as follows:
Header...
|
by: thomasp |
last post by:
Variables that I would like to make available to all forms and modules in my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. ...
|
by: Cerebral Believer |
last post by:
Hi I need help!
Forgive me I am a PHP newbie. I have a small script that enables me to send
a form from an HTML page. I want to use the HTML formatted form because the
design of my website is...
|
by: jopperdepopper |
last post by:
Hi,
finally giving php 5 a go, and going over the new approach to classes.
Can someone clarify the public, private and protected to me?
I quote the php manual: "The visibility of a property or...
|
by: quirk |
last post by:
I am trying to write a script where a page is populated with some maths questions, user answers them (it's timed but I've left this bit out), gets results on same page and ajax takes their score,...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |