Calling functions with hardcoded fieldnames?

  • Thread starter Thread starter Eych
  • Start date Start date
E

Eych

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.
 
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
 
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.
 
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=Microsoft.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(globalDBDataConnect.Dataset,
"UsersTable")

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top