Solved

How to  User a existed Control as a parameter of a function?

Posted on 2002-05-20
14
206 Views
Last Modified: 2011-09-20
I write a function, the type of one of the parameter is ComboBox , like this:
Private Sub AddDataToList(ByRef cmbTarget As ComboBox, ByRef strSql As String)
  .....
end sub

But How can I use a existed Control as a parameter when use this function? for example. I have a ComboBox in the form named cmbTypes, when I use it as "call AddDataToList(cmbTypes, strSql)", the compiler warns me with the message "the cmbTypes is not the name of a variable". How can I do?
0
Comment
Question by:Iaskyou
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 7023403
If the AddDataToList is in a module, it should declare as Public not private. However, make sure the ComboBox name passed to the Sub is absolute correct.

* In additional (just in case), use ByVal instead of ByRef.
0
 
LVL 1

Expert Comment

by:falconew
ID: 7023411
I think your code is correct. Please copy paste your detail code here.

Thanks.
0
 
LVL 2

Expert Comment

by:ventond
ID: 7025178
ByVal would make a copy of the combo box in memory and discard it when the sub ended. Probably not very usefull when you want to update the original.
0
 

Author Comment

by:Iaskyou
ID: 7025897
that are all codes.
the error occurs at "call AddDataToList(cmbTypes, strSql)".
cmbTypes is the name of the control which has existed in the form, not the declared variable.
0
 
LVL 2

Expert Comment

by:ventond
ID: 7026184
Are you sure you have this:
call AddDataToList(cmbTypes, strSql)

and not this?:
AddDataToList(cmbTypes, strSql)

0
 

Author Comment

by:Iaskyou
ID: 7026240
this is all codes:
--------------------------------------------------------
'//////////////////////////////////////////////////////////Option Explicit ' to enable proper syntax checking

' variable declarations with form scope
Private Sub Form_Load()

    'cmb1BookTypes is name of a existed ComboBox at Form
    Call AddDataToList(cmb1BookTypes, strSql)'Err Occurs
    Call AddDataToList(cmb2BookTypes, strSql)
End Sub

Private Sub AddDataToList(ByRef cmbTarget As ComboBox, ByRef strSql As String)
    Dim resData As New ADODB.Recordset
    Dim i As Integer
   
    cmbTarget.Clear
    On Error GoTo DBErrHandle

    'gblDBConn opened at gloabl.bas
    resData.Open strSql, gblDBConn
    i = 0
    While Not resData.EOF
        cmbTarget.AddItem (resdate(1))
        cmbTarget.ItemData(i) = CLng(resdate(0))
        i = i + 1
        resData.MoveNext
    Wend
    resData.Close
    If i > 0 Then
        cmbTarget.ListIndex = 0
    End If
   
DBErrHandle:
    MsgBox Err.Description
    Set resData = Nothing
End Sub


0
 
LVL 2

Expert Comment

by:ventond
ID: 7027451
Is it possible that your combobox is not a standard combo box? Perhaps a DbComboBox?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:MCummings111400
ID: 7028442
Try using Me.cmb1BookTypes...

Call AddDataToList(Me.cmb1BookTypes, strSql)

0
 
LVL 1

Expert Comment

by:falconew
ID: 7028762
I have tried your code on my computer and it runs normally, no warning message. Are you sure the error is on the combo box? Have you debug your code?
0
 
LVL 1

Accepted Solution

by:
falconew earned 50 total points
ID: 7028766
Here is my code

Private Sub Form_Load()
Dim sql As String
   'cmb1BookTypes is name of a existed ComboBox at Form
   Call AddDataToList(cmb1BookTypes, sql) 'Err Occurs
End Sub

Private Sub AddDataToList(ByRef cmbTarget As ComboBox, ByRef sql As String)
   Dim i As Integer
   cmbTarget.Clear
   On Error GoTo DBErrHandle

   'gblDBConn opened at gloabl.bas
   i = 0
   While i < 10
       cmbTarget.AddItem (i)
       cmbTarget.ItemData(i) = i
       i = i + 1
   Wend
   If i > 0 Then
       cmbTarget.ListIndex = 0
   End If
   Exit Sub
DBErrHandle:
   MsgBox Err.Description
End Sub

0
 
LVL 2

Expert Comment

by:ventond
ID: 7028985
The problem here is that your code that you have given us works. If it is stand alone. There is something else in your project that is causing this code in your project to fail. Right click on the cmb1BookTypes in your call statement. Choose Definition. If it takes you anywhere, chances are that is the problem.
0
 
LVL 3

Expert Comment

by:MCummings111400
ID: 7039563
Also double check the name of the control on your form. You may have misstyped the name in the properties box in the form design.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7818318
Hi Iaskyou,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept falconew's comment(s) as an answer.

Iaskyou, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7909830
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replace Formatted Numbers with text 9 61
Pull multiple cvs files into one access table 28 65
change vba from autofit to 13.5 width? 4 28
Access 2013 combo box not working 3 38
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now