Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag for Zambia asked on

How to convert the ? into length(bytes) in VBA Ms Access

I'm almost done with serial port data format parameters in Ms Access, now I need help on the last part of parameter listed below where the length byte is given ? how to treat it in binary conversion:

Fied                                                                     Length(Byte)

Content                                                                                        ?

so what do I put in the converter below under Length ????????? ( Could it be 0 or does ? represent somthing?):

Option Compare Database

Option Explicit

Public Function DecToBin(ByVal lngNumber As Long, Optional bytLength As Byte) As String

' Returns string that represents the binary expression for lngNumber.
' If bytLength is specified, returned string will be filled with
' leading zeroes up to this length.

  Dim strBin As String
  While lngNumber > 0
    strBin = (lngNumber Mod 2) & strBin
    lngNumber = lngNumber \ 2
  If bytLength > 0 Then
    strBin = Right(String(bytLength, "0") & strBin, bytLength)
  End If
  DecToBin = strBin

End Function 

Open in new window

* serial portMicrosoft Access

Avatar of undefined
Last Comment
Gustav Brock

8/22/2022 - Mon
Bill Prew

Are you saying you have cases where when you call this DecToBin() function it returns a question mark?

Are are you asking some other question about what value to place into the data packet that you are building to send out the serial port, and you are not sure what value it should be?  If this is the case I'd need to see the specification for the data packet you are building...


Computer basics:

Data is represented by bits. Depending on the used platform and software certain numbers of bits are grouped together. In the Intel-world this is typical

4 bits = 1nibble
2 nibble = 1 byte
2 byte = 1 word
2 word = 1 dword
4 word = 1 qword

As Long is a dword its binary representation has 32 bits. But your functions stops when it reaches the most significant set bit to one, cause leading zeros have no meaning in math. Thus the optional parameter is for controlling the necessary padding.

Instead of an optional parameter the first parameter should be variant and the padding should be used according to the given data type of the number.
Arana (G.P.)

bytLength is of byte datatype, it can hold values from 0 to 254.

if your lngNumber is 3  ( binary: 11), and your  bytLength  is 16 then according to the comments in your code you will have to fill the trailing spaces with zeroes

(16 length)
all the above is asuming I understood your question correctly.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Gustav Brock

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.