VB for MsAccess 2000 How to pass name of object to function

LostUser

New member
Local time
Today, 07:24
Joined
Apr 21, 2008
Messages
7
Hey there.

I am using MsAccess 2000 and am trying to add some extra usability features to a form so I have been using visual basic for Access.

What I am doing is this: Whenever a dropdown box has focus, I want it to dropdown automatically if the box is empty or if the down arrow key is pressed. I have worked it so that if there is already data in the box, the down arrow key drops the box down but doesn't move the selection down until you press the down arrow key again (prevents selecting another item accidentally on the first key press).

I have the code to do what I need but instead of adding a bigger section of the code to all the keydown properties, I am making a function that can be called.

(I don't know if 'property' is the right word for keydown)

Anyway, I want to get the sub name (AFP in this example) and pass it to the function farther down.

How do I do that?



Code:
Private Sub AFP_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 40 Then Call KeyDownOnce(onceflg, AFP)

End Sub

Code:
Public Function KeyDownOnce(onceflg As Integer, SubName As String)

msg = "onceflg= " + Str(onceflg) + " keycode= " + Str(KeyCode) + " subname= " + SubName
result = MsgBox(msg)


If KeyCode = 40 Then
    If onceflg = 0 Then
        SubName.Dropdown
        KeyCode = 0
        onceflg = 1
    Else
        SubName.Dropdown
    End If
End If

End Function

The msgbox line was just for troubleshooting.

The code I used above gives me an 'invalid qualifier' error.

I suppose, if I could work some code that runs in the form just checks when a dropdown box object is hit (or a list of names of dropdown boxes), that would require even less duplication of code but I don't know if that's workable without slowing things down.

Thanks in advance.
 
KeyDown is an event.

i'm betting that SubName.Dropdown is giving you the error. you are passing a string of the name but that's not enough for the code to know what you are talking about. if you wrote the code behind the form you would probably write Me.AFP.dropdown which includes the form (Me) which is probably what you need. i think you have to pass either me.afp or pass them separately. i've changed SubName to ctlName (Control Name). try:

Public Function KeyDownOnce(onceflg As Integer, frmName as string, ctlName As String)
then
Forms(frmName)!ctlName.dropdown

you might need more than that, i haven't tested. maybe:
Forms(frmName).Controls(ctlName).dropdown

somthing like that. or pass ctlName as a control instead of a string.
Public Function KeyDownOnce(onceflg As Integer, frmName as string, ctl As Control)

seriously consider renaming your controls (dropdowns, textboxes, etc). instead of AFP, which is vague, use cboAFP. dropdowns in Access are called comboboxes (cbo).
 
I think I see what you are saying. It needs to be able to identify the object fully (not sure I am using object right either). I'll give that a try, thanks.

I see what you mean about the naming too. Is that somewhat of a standard? CBO for combobox? Is there a place I can check out naming conventions for visual basic?
 
you got the idea exactly. and, "object" is correct. forms, reports, textboxes, labels, etc. are all objects and most of them have events you can write code for.

i checked and this works: Forms(frmName).Controls(ctlName).Dropdown
- when you pass frmName and ctlName (as strings) use: Me.Name and Me.YourControl.Name.

also works: Public Function KeyDownOnce(onceflg As Integer, ctl As Control) (frmName as string not required)
- then you can simply pass Me.YourControl (without .Name) and use ctl.dropdown in the function's code. (you might then want to check and make sure that the control is actually a combobox, or .dropdown would give you an error (but i suppose you'll only use this on comboboxes...).

some naming conventions:
http://en.wikipedia.org/wiki/Leszynski_naming_convention
http://support.microsoft.com/defaul...port/kb/articles/q173/7/38.asp&NoWebContent=1
 
Last edited:
Hello and back again, sorry I haven't been back to this topic but I've been busy at work.

I wasn't able to get a public function to work correctly to cause the dropdown box to drop down in the correct situation...it may have had to do something with reading the keycode (40) for the down arrow.

Anyway, I put non-specific code into all the KeyDown events that will work but I still would like to be able to just do a call.

Is there a way to detect if the .dropdown is in effect? ie, dropped down or not? I've tried different things to check this but can't find it.

Here is the code I have that works like I need but I still would like to use just a call and maybe get rid of the onceflg variable (if a dropdown can be detected).

Code:
Private Sub AFP_GotFocus()
 
If IsNull(Screen.ActiveControl) Then
    Screen.ActiveControl.Dropdown
    onceflg = 1
End If

End Sub

Code:
If keycode = 40 Then
    If onceflg = 0 Then
        Screen.ActiveControl.Dropdown
        keycode = 0
        onceflg = 1
    Else
        Screen.ActiveControl.Dropdown
    End If
End If



Thanks again.
 
Hello and back again, sorry I haven't been back to this topic but I've been busy at work.

I wasn't able to get a public function to work correctly to cause the dropdown box to drop down in the correct situation...it may have had to do something with reading the keycode (40) for the down arrow.

Anyway, I put non-specific code into all the KeyDown events that will work but I still would like to be able to just do a call.

Is there a way to detect if the .dropdown is in effect? ie, dropped down or not? I've tried different things to check this but can't find it.

Here is the code I have that works like I need but I still would like to use just a call and maybe get rid of the onceflg variable (if a dropdown can be detected).

Code:
Private Sub AFP_GotFocus()
 
If IsNull(Screen.ActiveControl) Then
    Screen.ActiveControl.Dropdown
    onceflg = 1
End If
 
End Sub

Code:
Private Sub AFP_KeyDown(keycode As Integer, Shift As Integer)
 
If keycode = 40 Then
    If onceflg = 0 Then
        Screen.ActiveControl.Dropdown
        keycode = 0
        onceflg = 1
    Else
        Screen.ActiveControl.Dropdown
    End If
End If
 
End Sub



Thanks again.
 
you can pass a sub name with eval function

its like vba - you have t produce a string representing the function you want to call with parameters and brackets

so mystring is "myfunc(myparam1, myparam2)"

THEN you can call

eval(mystring) , and it will call your function dynamically.

is that what you mean?
 
Thanks for the reply gth. I initially thought I needed to pass the subname but what I really needed was to access the current control ... which I can now. Passing the subname is nice to know how to do though.

My current situation is that I need to be able to detect if a combo box is dropped down or not. If this isn't possible, I'll just need to use a variable as a flag to indicate when I've forced the dropdown.

I want to be able to make the same call from either the onfocus or keydown events and let the function figure out the rest.
 

Users who are viewing this thread

Back
Top Bottom