Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Function/Aub Issues 1

Status
Not open for further replies.

kermitforney

Technical User
Mar 15, 2005
374
US
Having some issues with the code below"
Code:
Public Function fncInt2Bin(n As Long, Optional bReverse = False) As String
    Dim m
    m = n
    Do
        If bReverse Then
            Dec2Bin = Dec2Bin & m Mod 2
        Else
            Dec2Bin = m Mod 2 & Dec2Bin
        End If
        m = Int(m / 2)
    Loop Until m <= 0
End Function

End Function

Public Sub ControlNumber()
Dim InvoiceID As int32
Dim ControlNumber As Long

InvoiceID = Me.txtInvoiceID.Text
ControlNumber = InvoiceID(fcnInt2Bin)

Me.txtControlNumber = ControlNumber

End Sub

I am trying to assign the text box (txtControlNumber) to the variable (ControlNumber), but it is not working for some reason. Do I have to input information into the Control Source of the text box?? Help!!!

Thanks in advance!! :eek:)
 
How about:

Code:
Public Function fncInt2Bin(n As Long, Optional bReverse = False) As String
    Dim m
    m = n
    Do
        If bReverse Then
            Dec2Bin = Dec2Bin & m Mod 2
        Else
            Dec2Bin = m Mod 2 & Dec2Bin
        End If
        m = Int(m / 2)
    Loop Until m <= 0
    
    fncInt2Bin = Dec2Bin
End Function



Public Sub ControlNumber()
Dim InvoiceID As Long 'As int32
Dim ControlNumber 'As Long

'.Text is only available if the control has focus
InvoiceID = Me.txtInvoiceID
ControlNumber = fncInt2Bin(InvoiceID)

Me.txtControlNumber = ControlNumber

End Sub
 
Still nothing ? ? ? Are you sure that the control source for the text box does not need to be set?

I am still getting a blank text box without any errors.
 
What does txtInvoiceID contain? If it does not have a value, then nothing will be returned. How you do this is very much up to you. You can enter a value manually or you can use a control source. If you enter a value or update, be sure to move the focus before running any code.
 
Would it make a difference if this was in the Report's On Open Event Procedure??
 
Yes, it would. Reports are quite fussy about where the code goes. You could try the detail format event.
 
Tried detail to no avail . . . :eek:(
Not sure why it isn't populating the text box.
 
Try:

SomeTextbox="Hi"

To ensure you have the right event.
 
That doesn't work either.
I tried the Event Procedures in:

Page Header
On Format
Detail
On Format
Report
On Open

Nothing seems to populate this text box, I may just have to start from scratch. :eek:(
 
Let me be quite clear on this. You have created an unbound textbox and checked that you have the correct name and the detail format event does not populate the textbox. Is this correct?

Here is an example:
Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    Me.txtTest = "Hi"
End Sub
 
Yes, Yes and No I did not place it in the Detail_Format Event. To be specific the text box is in the Page Header, if that makes any difference. I apologize for my ineptitude, but I am a fish out of water when it comes to VBA.

Error Message: Ambiguous name detected Detail_Format
 
Error Message: Ambiguous name detected Detail_Format
This means that you have two Detail_Format procedures, rename or delete one of them.

Then:

Code:
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
Me.txtTest = "hi"
End Sub
 
Works now, going to apply the rest of the code, thanks Remou!!!
 
Ok, after trying to implement the changes I have a new error message.
"Compile error: Variable not defined",

for the Function:fcnInt2Bin in the following code.

Code:
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)

    Dim InvoiceID As Long
    Dim ControlNumber As Long

    InvoiceID = Me.txtInvoiceID
    ControlNumber = InvoiceID([red]fcnInt2Bin[/red])

    Me.txtControlNumber = ControlNumber

End Sub
[\code]
 
You have the line the wrong way round. You must pass an argument to the function (fncInt2Bin), not pass a function to an argument:

ControlNumber = fncInt2Bin(InvoiceID)
 
When I comment out the:
Code:
ControlNumber = fcnInt2Bin(InvoiceID)

and change this line
Code:
Me.txtControlNumber = ControlNumber
to
Code:
Me.txtControlNumber = InvoiceID 'ControlNumber

it works fine . . .

Something is incorrect in the function call . . I think. :eek:)
 
When I use this line:
Code:
ControlNumber = fncInt2Bin(InvoiceID)
I get this Compile error:
Sub or function not defined.
 
Where did you put this code?

Code:
Public Function fncInt2Bin(n As Long, Optional bReverse = False) As String
    Dim m
    m = n
    Do
        If bReverse Then
            Dec2Bin = Dec2Bin & m Mod 2
        Else
            Dec2Bin = m Mod 2 & Dec2Bin
        End If
        m = Int(m / 2)
    Loop Until m <= 0
    
    fncInt2Bin = Dec2Bin
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top