Creating Function

Crash1hd

Registered CyberGeek
Local time
Today, 10:36
Joined
Jan 11, 2004
Messages
143
If I am correct useing a function is to cut down on having to write the samething over and over again! right?

anyhow if I am wrong please correct and put me on the right path to what is that does that ok below is some code that I am having trouble with

so I have this code that works
Private Sub EnhNameList2_GotFocus()
Enhnamelist2.Dropdown
End Sub

but I dont want to write that 7 times one for each EnhNameList1-7_GotFocus() ext... so I thought I could use a function

Function dropd(num)
Enhnamelist(num).Dropdown
End Function

Private Sub EnhNameList2_GotFocus()
dropd (2)
End Sub

and then just call drop(2) in the GotFocus() but that doesnt work???
 
Hi,

In a way you have the right idea about the use of functions. There is a good guide to functions in the Access Help and loads of stuff on here.

The point i would make regarding your function is that you are simply replacing one line of code with another.

Code:
Private Sub EnhNameList2_GotFocus()

[B]Enhnamelist2.Dropdown[/B]

End Sub

'with

Private Sub EnhNameList2_GotFocus()

[B] dropd (2) [/B]

End Sub
This isn't really efficient as you end up are running more lines of code {using the function} than you would normally do to achieve the same result.

If you really want to do this you could try the following.
I can't promise it will work as i haven't tested it.

Code:
 ' Private will only work in the form module that it is written in.
' Pass the listbox that you are working with

Private Function dropd(LstBox as Listbox) 

LstBox.Dropdown

End Function

call the procedure

Code:
Private Sub EnhNameList2_GotFocus()

dropd Me.EnhNameList2

End Sub


Another way may be


Code:
Private Function dropd(Frm as Form, ListNum as Integer) 

Frm.Controls("EnhNameList" & ListNum).Dropdown

End Function

call the procedure

Code:
Private Sub EnhNameList2_GotFocus()

dropd Me,2

End Sub

Either way you end up running more code which ends up taking more time and memory.

HTH

The Stoat
 
Last edited:
Thankyou for all the info the reason I was trying to get the function to work was I actually going to use the function on this code

Code:
Private Sub EnhNameList_AfterUpdate()
    If Me.Enhnamelist <> "" And Me.EnhLevelList <> "" Then

'Price to Sell At
        Me.EnhPriceSellTxt = _
            DMax("enhpricesell", "enhancements", "enhname = '" & Enhnamelist & "' AND " & "enhlevel =" & EnhLevelList)
        If Me.EnhPriceSellTxt <> "" Then

'Store to Sell At
            Me.EnhStoreSellTxt = _
                DMax("enhStore", "enhancements", "enhname = '" & Enhnamelist & "' AND " & " enhpriceSell =" & EnhPriceSellTxt & " AND " & "enhlevel =" & EnhLevelList)
            mySQL = "Select enhStore from enhancements Where enhname = '" & Enhnamelist & "' AND " & " enhpriceSell =" & EnhPriceSellTxt & " AND " & "enhlevel =" & EnhLevelList
                EnhStoreSellTxt.RowSource = mySQL

        Else
            Me.EnhPriceSellTxt = ""
            Me.EnhPriceBuyTxt = ""
            EnhStoreSellTxt.RowSource = ""
            EnhStoreBuyTxt.RowSource = ""
            Me.EnhStoreSellTxt = ""
            Me.EnhStoreBuyTxt = ""
        End If

'Price to Buy At
        Me.EnhPriceBuyTxt = _
            DMin("enhpricebuy", "enhancements", "enhname = '" & Enhnamelist & "' AND " & "enhlevel =" & EnhLevelList)
        If Me.EnhPriceBuyTxt <> "" Then

'Store to Buy At
            Me.EnhStoreBuyTxt = _
                DMax("enhStore", "enhancements", "enhname = '" & Enhnamelist & "' AND " & " enhpriceBuy =" & EnhPriceBuyTxt & " AND " & "enhlevel =" & EnhLevelList)
            mySQL = "Select enhStore from enhancements Where enhname = '" & Enhnamelist & "' AND " & " enhpriceBuy =" & EnhPriceBuyTxt & " AND " & "enhlevel =" & EnhLevelList
                EnhStoreBuyTxt.RowSource = mySQL

        Else
            Me.EnhPriceSellTxt = ""
            Me.EnhPriceBuyTxt = ""
            EnhStoreSellTxt.RowSource = ""
            EnhStoreBuyTxt.RowSource = ""
            Me.EnhStoreSellTxt = ""
            Me.EnhStoreBuyTxt = ""
        End If
    Else
        Me.EnhPriceSellTxt = ""
        Me.EnhPriceBuyTxt = ""
        EnhStoreSellTxt.RowSource = ""
        EnhStoreBuyTxt.RowSource = ""
        Me.EnhStoreSellTxt = ""
        Me.EnhStoreBuyTxt = ""
    End If
End Sub

And anywhere you get a Me.name there will be a number after it! I wanted to also get the idea of it :)
 

Users who are viewing this thread

Back
Top Bottom