How to ? 
Author Message
 How to ?

I am about to extract bytes from a certain integer value.
An if i write 14200,05 i shall get this :
01, 42, 00, and 05,
With 950 i shall get 00 09 50 00 also with 950,00 :-)
With 1247,36 i shall get 00 12 47 36
I declare 4 variables F1, F2, F3 and F4.
Max is 30000,00 and min is 130,00

When i am using this code i make the number 950 correct, but is it wrong to
use "select case" here ?

Private Sub Command1_Click()
Dim Frekvens As Double
Dim grunn As Double
Frekvens = txtFrekvens
grunn = Frekvens * 100   ' to make 130,05 possible :-)

Dim F1 As String
Dim F2 As String
Dim F3 As String
Dim F4 As String
Dim Fa As String
Dim Fb As String
Dim Fc As String
Dim Fd As String
Dim Fe As String
Dim Ff As String
Dim Fg As String
Select Case grunn
    Case Is >= 13000 And grunn < 100000
        F4 = Right(grunn, 2)
        lblF4 = F4
        F1 = "0" & "0"
        lblF1 = F1
        Fa = Left(grunn, 1)
        F2 = "0" & Fa
        lblF2 = F2
        Fb = Left(grunn, 3)
        F3 = Right(Fb, 2)
        lblF3 = F3
    Case Is >= 100000 And grunn < 1000000
        F4 = Right(grunn, 2)
        lblF4 = F4
        F1 = "0" & "0"
        lblF1 = F1
        F2 = "0" & Left(grunn, 3)
        lblF2 = F2
        Fc = Left(grunn, 4)
        F3 = Right(Fc, 2)
        lblF3 = F3
    Case Is >= 1000000 And grunn <= 3000000
        F4 = Right(grunn, 2)
        lblF4 = F4
        Fd = Left(grunn, 2)
        F1 = Fd
        lblF1 = F1
        Fe = Left(grunn, 3)
        F2 = Right(Fe, 2)
        lblF2 = F2
        Ff = Left(grunn, 5)
        F3 = Right(Ff, 2)
        lblF3 = F3
End Select
Form1.txtFrekvens.SetFocus
End Sub

Where am i off the track ?

Thanks anyone !



Fri, 23 Feb 2001 03:00:00 GMT  
 How to ?
There is a much easier way to do this, using the Format function to fix the
number of decimal places before and after the decimal separator:

    Dim grunn As Double
    Dim F1 As String, F2 As String
    Dim F3 As String, F4 As String
    Dim sGrunn As String
    grunn = 14200,05
    sGrunn = Format$(grunn, "000000.00")
    F1 = Left$(sGrunn, 2)
    F2 = Mid$(sGrunn, 3, 2)
    F3 = Mid$(sGrunn, 5, 2)
    F4 = Right$(sGrunn, 2)

Lee Weiner
weiner AT fuse DOT net


Quote:

>I am about to extract bytes from a certain integer value.
>An if i write 14200,05 i shall get this :
>01, 42, 00, and 05,
>With 950 i shall get 00 09 50 00 also with 950,00 :-)
>With 1247,36 i shall get 00 12 47 36
>I declare 4 variables F1, F2, F3 and F4.
>Max is 30000,00 and min is 130,00

>When i am using this code i make the number 950 correct, but is it wrong to
>use "select case" here ?

>Private Sub Command1_Click()
>Dim Frekvens As Double
>Dim grunn As Double
>Frekvens = txtFrekvens
>grunn = Frekvens * 100   ' to make 130,05 possible :-)

>Dim F1 As String
>Dim F2 As String
>Dim F3 As String
>Dim F4 As String
>Dim Fa As String
>Dim Fb As String
>Dim Fc As String
>Dim Fd As String
>Dim Fe As String
>Dim Ff As String
>Dim Fg As String
>Select Case grunn
>    Case Is >= 13000 And grunn < 100000
>        F4 = Right(grunn, 2)
>        lblF4 = F4
>        F1 = "0" & "0"
>        lblF1 = F1
>        Fa = Left(grunn, 1)
>        F2 = "0" & Fa
>        lblF2 = F2
>        Fb = Left(grunn, 3)
>        F3 = Right(Fb, 2)
>        lblF3 = F3
>    Case Is >= 100000 And grunn < 1000000
>        F4 = Right(grunn, 2)
>        lblF4 = F4
>        F1 = "0" & "0"
>        lblF1 = F1
>        F2 = "0" & Left(grunn, 3)
>        lblF2 = F2
>        Fc = Left(grunn, 4)
>        F3 = Right(Fc, 2)
>        lblF3 = F3
>    Case Is >= 1000000 And grunn <= 3000000
>        F4 = Right(grunn, 2)
>        lblF4 = F4
>        Fd = Left(grunn, 2)
>        F1 = Fd
>        lblF1 = F1
>        Fe = Left(grunn, 3)
>        F2 = Right(Fe, 2)
>        lblF2 = F2
>        Ff = Left(grunn, 5)
>        F3 = Right(Ff, 2)
>        lblF3 = F3
>End Select
>Form1.txtFrekvens.SetFocus
>End Sub

>Where am i off the track ?

>Thanks anyone !



Fri, 23 Feb 2001 03:00:00 GMT  
 How to ?
Hi Torbj?rn,

I agree with the previous poster that there is an easier way to do what you
want. But the reason your approach did not work is that the Case statements in
a select must be a single value as in:

    select (a)
        Case 1:
            'Handle Case 1
        Case 2:
            'Handle Case 2
        Case 3:
            'Handle Case 3
    end select

to do a range check you should use if then else if else if as in:

    if (grunn >= 13000) And (grunn < 100000) then
        F4 = Right(grunn, 2)
        lblF4 = F4
        F1 = "0" & "0"
        lblF1 = F1
        Fa = Left(grunn, 1)
        F2 = "0" & Fa
        lblF2 = F2
        Fb = Left(grunn, 3)
        F3 = Right(Fb, 2)
        lblF3 = F3
    else
        if (Grunn >= 100000) And (grunn < 1000000) then
            F4 = Right(grunn, 2)
            lblF4 = F4
            F1 = "0" & "0"
            lblF1 = F1
            F2 = "0" & Left(grunn, 3)
            lblF2 = F2
            Fc = Left(grunn, 4)
            F3 = Right(Fc, 2)
            lblF3 = F3
        else
            if (Grunn >= 1000000) And (grunn <= 3000000) then
               F4 = Right(grunn, 2)
                lblF4 = F4
                Fd = Left(grunn, 2)
                F1 = Fd
                lblF1 = F1
                Fe = Left(grunn, 3)
                F2 = Right(Fe, 2)
                lblF2 = F2
                Ff = Left(grunn, 5)
                F3 = Right(Ff, 2)
                lblF3 = F3
            end if
        end if
      endif

    I didn't check your logic within each if block but this is how you should
test your various conditions rather than using a Select case statement. Hope
this helps

--
-Dick Christoph

http://www1.minn.net/~dchristo

Quote:

> I am about to extract bytes from a certain integer value.
> An if i write 14200,05 i shall get this :
> 01, 42, 00, and 05,
> With 950 i shall get 00 09 50 00 also with 950,00 :-)
> With 1247,36 i shall get 00 12 47 36
> I declare 4 variables F1, F2, F3 and F4.
> Max is 30000,00 and min is 130,00

> When i am using this code i make the number 950 correct, but is it wrong to
> use "select case" here ?

> Private Sub Command1_Click()
> Dim Frekvens As Double
> Dim grunn As Double
> Frekvens = txtFrekvens
> grunn = Frekvens * 100   ' to make 130,05 possible :-)

> Dim F1 As String
> Dim F2 As String
> Dim F3 As String
> Dim F4 As String
> Dim Fa As String
> Dim Fb As String
> Dim Fc As String
> Dim Fd As String
> Dim Fe As String
> Dim Ff As String
> Dim Fg As String
> Select Case grunn
>     Case Is >= 13000 And grunn < 100000
>         F4 = Right(grunn, 2)
>         lblF4 = F4
>         F1 = "0" & "0"
>         lblF1 = F1
>         Fa = Left(grunn, 1)
>         F2 = "0" & Fa
>         lblF2 = F2
>         Fb = Left(grunn, 3)
>         F3 = Right(Fb, 2)
>         lblF3 = F3
>     Case Is >= 100000 And grunn < 1000000
>         F4 = Right(grunn, 2)
>         lblF4 = F4
>         F1 = "0" & "0"
>         lblF1 = F1
>         F2 = "0" & Left(grunn, 3)
>         lblF2 = F2
>         Fc = Left(grunn, 4)
>         F3 = Right(Fc, 2)
>         lblF3 = F3
>     Case Is >= 1000000 And grunn <= 3000000
>         F4 = Right(grunn, 2)
>         lblF4 = F4
>         Fd = Left(grunn, 2)
>         F1 = Fd
>         lblF1 = F1
>         Fe = Left(grunn, 3)
>         F2 = Right(Fe, 2)
>         lblF2 = F2
>         Ff = Left(grunn, 5)
>         F3 = Right(Ff, 2)
>         lblF3 = F3
> End Select
> Form1.txtFrekvens.SetFocus
> End Sub

> Where am i off the track ?

> Thanks anyone !



Fri, 23 Feb 2001 03:00:00 GMT  
 How to ?
Have you looked into using the Format() command?

strTemp = Format$(input, "000000,00")
If Val(strTemp) > 30000 Or Val(strTemp) < 130 Then
    Exit Sub
End If
lblF1.Caption = Left$(strTemp, 2)
lblF2.Caption = Mid$(strTemp, 3, 2)
lblF3.Caption = Mid$(strTemp, 5, 2)
lblF4.Caption = Right$(strTemp, 2)

--
John Tabor

http://members.bellatlantic.net/~jftabor
___________

Quote:

>When i am using this code i make the number 950 correct, but is it wrong to
>use "select case" here ?
>Where am i off the track ?



Fri, 23 Feb 2001 03:00:00 GMT  
 How to ?

Dick Christoph skrev i meldingen <

Snip...

Thank you for this one Richard, it worked nicely !

Using MSComm in VB4.0 i would like to send each of theese values, F1 to F4
and another number in Hex out on com1/2.

How ?

Comm1.Output = lblF1 + lblF2 etc... ?
Or how else ?



Sun, 25 Feb 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software