How To Write Code 101

Randomblink

The Irreverent Reverend
Local time
Today, 14:56
Joined
Jul 23, 2001
Messages
279
Can someone help me please?
I am wondering if there is a trick to this...
Something that will make this easier for my code-jumbled brain to understand or use...

I have been given the following code...(by The_Doc_Man)

Private Function GetUserDepartment(sUser As String)
Dim sDept As Integer
sDept = DLookup("[DEPT_ID]", "tbl_Employee", "[Empl_NTLogon]='" & sUser & "'")
GetUserDepartment = DLookup("[DEPT_NAME]", "tbl_Department", "[DEPT_ID]=" & sDept & "")
End Function

I am trying to genericize it (yes, I just made up a word!)...
I want to recreate what this Function above does, but NOT restrict it to looking for the UserDepartment...
I am slowly piecing something together like below...

Private Function UserDetails(trgt_tbl As String, trgt_tbl_fld As String, trgt_tbl_crit As String, trgt_scent As String, trgt As Variant, fnd_tbl As String, fnd_tbl_fld As String)
' trgt_tbl = tbl_Employee
' trgt_tbl_fld = DEPT_ID
' trgt_tbl_crit = Empl_NTLogon
' trgt_scent = sUser
' trgt = sDept
' fnd_tbl = tbl_Department
' fnd_tbl_fld = DEPT_NAME
' trgt_tbl_fld = DEPT_ID
' Function that grabs the needed data GetUserDepartment
trgt_scent = GetNTUser
trgt = DLookup(trgt_tbl_fld, trgt_tbl, "trgt_tbl_crit&""='" & trgt_scent & "'")
GetUserDepartment = DLookup(fnd_tbl_fld, fnd_tbl, "trgt_tbl_fld & "=" & trgt & "")
End Function

When it dawns on me...
Having had no formal training in VB, excluding a class at the local community college by an imbecile, I am not sure if there is a standard way of putting something like this into action...

If you can help me, I would appreciate it...
 
Sorry, these won't work

trgt = DLookup(trgt_tbl_fld, trgt_tbl, "trgt_tbl_crit&""='" & trgt_scent & "'")
GetUserDepartment = DLookup(fnd_tbl_fld, fnd_tbl, "trgt_tbl_fld & "=" & trgt & "")

The variable names in quotes will not be resolved to the parameter values.

You need to build a string that contains the criteria statememt.

Something like:
strCriteria = trgt_tbl_crit & "='" & trgt_scent

Then
GetUserDepartment = DLookup(fnd_tbl_fld, fnd_tbl, strCriteria)
would work.

RichM
 
Ok, this is what I am moving to

http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=30258

If you could check out this thread you will see maybe what I want to do...

I would like to create a function with all the possible arguments I could search for using the Users NT Logon so that as I call the function, it would dropdown a list of fields I can search for in the tbl_Employee...
 

Users who are viewing this thread

Back
Top Bottom