Solved VBA Function with multiple outputs (1 Viewer)

silversun

Registered User.
Local time
Today, 11:49
Joined
Dec 28, 2012
Messages
204
Hello,
I searched a lot and came up with a function that is not working the way I wanted it.
I have a simple form with an input textbox (txt_input) and a button (btn_convert) that is meant to run my function and assign the outputs (decodedArray(5)) to my output textboxes (TextBox0 through TextBox4).
Function is to take a number (less than 32) as decimal and decodes it to a 5-bit binary while all bits will be displayed in a set of textboxes. (each bit in an individual textbox)
I am trying to use an array as my output and a single input named myDecimal.
Here are my codes:
Code:
Public Function convertDec2Bin(myDecimal) As Variant

Dim n As Integer
Dim decodedArray(5) As Variant

n = myDecimal

decodedArray(0) = n Mod 2
n = n / 2
Me.TextBox0 = decodedArray(0)

decodedArray(1) = n Mod 2
n = n / 2
Me.TextBox1 = decodedArray(1)

decodedArray(2) = n Mod 2
n = n / 2
Me.TextBox2 = decodedArray(2)

decodedArray(3) = n Mod 2
n = n / 2
Me.TextBox3 = decodedArray(3)

decodedArray(4) = n Mod 2
n = n / 2
Me.TextBox4 = decodedArray(4)

convertDec2Bin = decodedArray
End Function

And this is what I have under my button's On-Click event:

Code:
Private Sub btn_convert_Click()

Dim decodedArray As Variant

myDecimal = Me.txt_input

packaging = convertDec2Bin(myDecimal)

End Sub

I appreciate if you help me to fix this code and explain what you are doing.
Thanks
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 19:49
Joined
Feb 19, 2013
Messages
16,605
perhaps start with what you are trying to do? an example value and the values returned and expected output would be a help.

if n =32
then 32 mod 2=0
n/2=16
16 mod 2=0
etc
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:49
Joined
Feb 28, 2001
Messages
27,163
There are many ways to approach this but I will save you at least one problem. A function SHOULD be single-valued. But if you make the function into a subroutine for which the "returned value array" is an array passed ByRef, you can deposit each bit that you develop into a different array element. When you return from the SUB (not function), that ByRef qualifier on the array means that the CALLER's copy of the array has been updated.
 

silversun

Registered User.
Local time
Today, 11:49
Joined
Dec 28, 2012
Messages
204
perhaps start with what you are trying to do? an example value and the values returned and expected output would be a help.

if n =32
then 32 mod 2=0
n/2=16
16 mod 2=0
etc
I want to enter any number les than 32 manually in my input textbox and have the binary codes in output textboxes.
For example if I enter 27 I am expecting to see: 11011, each bit in an individual textbox. This code I have here outputs nothing! :( :(
I can upload my simple DB once I get home later on today.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 14:49
Joined
May 21, 2018
Messages
8,527
Code:
Function CBin(ByVal n As Double) As String
    If n = 0 Then
        CBin = 0
    ElseIf n > 0 Then
        Dim i As Double
        Dim c As Long
        i = 2 ^ CLng(Log(n) / Log(2) + 0.1)
        Do While i >= 1
            c = Fix(n / i)
            CBin = CBin & c
            n = n - i * c
            i = i / 2
        Loop
    End If
End Function
Code:
Public Sub TestD2B()
  Dim x As Double
  Dim bin As String
  x = 29
  bin = CBin(x)
  Debug.Print bin
  Debug.Print CInt(Mid(bin, 1, 1))
  Debug.Print CInt(Mid(bin, 2, 1))
  Debug.Print CInt(Mid(bin, 3, 1))
  Debug.Print CInt(Mid(bin, 4, 1))
  Debug.Print CInt(Mid(bin, 5, 1))
   Debug.Print CInt(Mid(bin, 6, 1))
End Sub
in my test replace the debug.print with the correct textbox
me.txt2^5 = CInt(Mid(bin, 1, 1))
me.txt2^4 = CInt(Mid(bin, 2, 1))
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:49
Joined
May 7, 2009
Messages
19,230
Code:
Private Sub btn_convert_Click()
Dim sRet As String
Dim i As Integer
If Val(Me.txt_Input & "") > 32 Then
    Msgbox "Integer value should be less than 32"
    Exit Sub
End If
sRet = Format$(fncDec2Bin(Me.txt_Input), "00000")
For i = 0 to 4
    Me("Textbox" & i) = Mid$(sRet, i+1, 1)
Next
End Sub

on a Separated Module:
Code:
Public Function fncDec2Bin(pdblValue As Double) As String
    Dim sRet As String
    sRet = (pdblValue Mod 2) & sRet
    pdblValue = pdblValue \ 2
    While pdblValue > 0
        sRet = (pdblValue Mod 2) & sRet
        pdblValue = pdblValue \ 2
    Wend
    fncDec2Bin = Format$(sRet, Switch(Len(sRet) < 5, "0000", Len(sRet) < 9, "00000000", Len(sRet) < 13, "000000000000", Len(sRet) < 17, "0000000000000000"))
End Function
 

silversun

Registered User.
Local time
Today, 11:49
Joined
Dec 28, 2012
Messages
204
Code:
Private Sub btn_convert_Click()
Dim sRet As String
Dim i As Integer
If Val(Me.txt_Input & "") > 32 Then
    Msgbox "Integer value should be less than 32"
    Exit Sub
End If
sRet = Format$(fncDec2Bin(Me.txt_Input), "00000")
For i = 0 to 4
    Me("Textbox" & i) = Mid$(sRet, i+1, 1)
Next
End Sub

on a Separated Module:
Code:
Public Function fncDec2Bin(pdblValue As Double) As String
    Dim sRet As String
    sRet = (pdblValue Mod 2) & sRet
    pdblValue = pdblValue \ 2
    While pdblValue > 0
        sRet = (pdblValue Mod 2) & sRet
        pdblValue = pdblValue \ 2
    Wend
    fncDec2Bin = Format$(sRet, Switch(Len(sRet) < 5, "0000", Len(sRet) < 9, "00000000", Len(sRet) < 13, "000000000000", Len(sRet) < 17, "0000000000000000"))
End Function
Thank you all and thanks again to Arnelgp. Your code is the solution to my question.
Have a nice day
Masoud
 

Users who are viewing this thread

Top Bottom