By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Calling functions with hardcoded fieldnames?

P: n/a
I have a module which has a function that returns values from a table.

e.g.
GetFromUsersTable(userID,"UserName")
GetFromUsersTable(userID,"UserDept")
etc.

depending on the userID passed, it will return the field requested.

Question is where/how do I define constants that I can use in place of
"UserName", "UserDept", etc., in case these field names change?

What is the correct syntax and where could I place them (in the main
module, in their own module, a class)?
thanks.

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I would use an "Enum" for the fields like: (not sure of exact syntax)

Public Enum myFields
UserName
UserDept
End Enum

Dim FieldNeeded as myFields

GetFromUsersTable(UserId as string, FieldNeeded as myFields)
Select Case FieldNeeded
case UserName
'return the users name
case UserDept
'return user Dept
End Select
End Sub

"Eych" wrote:
I have a module which has a function that returns values from a table.

e.g.
GetFromUsersTable(userID,"UserName")
GetFromUsersTable(userID,"UserDept")
etc.

depending on the userID passed, it will return the field requested.

Question is where/how do I define constants that I can use in place of
"UserName", "UserDept", etc., in case these field names change?

What is the correct syntax and where could I place them (in the main
module, in their own module, a class)?
thanks.

Nov 21 '05 #2

P: n/a
No one on this NG wrote your 'GetFromUsersTable' class, so it's pretty tough
for us to help you.

If you post some of the code from that class, we may be able to help...

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Eych" <ey******@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I have a module which has a function that returns values from a table.

e.g.
GetFromUsersTable(userID,"UserName")
GetFromUsersTable(userID,"UserDept")
etc.

depending on the userID passed, it will return the field requested.

Question is where/how do I define constants that I can use in place of
"UserName", "UserDept", etc., in case these field names change?

What is the correct syntax and where could I place them (in the main
module, in their own module, a class)?
thanks.

Nov 21 '05 #3

P: n/a
Below is some code from the function, it searches for the userID and
returns the field requested.

I was asking because I just didn't want to hardcode the requested
field.

I could do the Enum like:

Public Enum dbFields as String
UserName = "UserName"
UserDept = "UserDept"
End Enum

and place this in the module where I have my Public Sub main()

I guess I'm wondering if there is a more conventional way, such as
creating project-wide constants so that
I don't have to declare a dbFields each time I want to use a field.
Then again, I could create an Enum for each table, where each Enum
contains the fields for that table...hmmm...

Function GetFromUsersTable(ByVal user_ID As String, ByVal fieldName As
String) As String

Dim globalDBDataConnect As New DataConnect 'class I created in
easing creation of datasets
Dim returnValue As String = ""

Try
globalDBDataConnect.Connection = New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source="C:\maindb.mdb")
globalDBDataConnect.Adapter = New
System.Data.OleDb.OleDbDataAdapter("select * from UsersTable where
UserID = '" & user_ID & "'", globalDBDataConnect.Connection)
globalDBDataConnect.Dataset = New DataSet

globalDBDataConnect.Adapter.Fill(globalDBDataConne ct.Dataset,
"UsersTable")

If globalDBDataConnect.Dataset.Tables(0).Rows.Count > 0
Then
'status found
returnValue =
(globalDBDataConnect.Dataset.Tables(0).Rows(0)(fie ldName))
End If
Return returnValue

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.