Format Function

Format Function

Anonymous
Not applicable
380 Views
10 Replies
Message 1 of 11

Format Function

Anonymous
Not applicable
Can you use the Format Function on a variable that is declared as an
integer?  If not, how can I get an Integer variable to hold the results of
a string with the following code:

 

    Rnum2 = txtStrtNum.Text  'in this case
txtstrtnum.text = A001
    Cnt = Mid$(Rnum2, 2, 4) 'Since
the first two characters are zeros, Cnt changes it too an integer? and Cnt now =
"1" and I want it to = "001"
   

How do I force Cnt to = "001"

 

Thanx,

 

Rob
0 Likes
381 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
You could make the string "A00" & Cnt for Cnt
< 10, then "A0" for Cnt 10-99, etc.


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">

Can you use the Format Function on a variable that is declared as an
integer?  If not, how can I get an Integer variable to hold the results
of a string with the following code:

 

    Rnum2 = txtStrtNum.Text  'in this case
txtstrtnum.text = A001
    Cnt = Mid$(Rnum2, 2, 4)
'Since the first two characters are zeros, Cnt changes it too an integer? and
Cnt now = "1" and I want it to = "001"
   

How do I force Cnt to = "001"

 

Thanx,

 

Rob
0 Likes
Message 3 of 11

Anonymous
Not applicable
Assuming Rnum2 is a String variable and always in the format ANNN
(alpha, numeric, numeric, numeric), Mid$(Rnum2, 2) wil return "001"
(in the case of "A001").

If you're not sure about the value being properly zero-padded,
Format(Mid$(Rnum2, 2), "000") will return "001", even if the original
text was "A1"

Hope this helps.

--
http://www.acadx.com
Win a free autographed copy of
"AutoCAD 2000 VBA Programmer's Reference"
by Joe Sutphin

"Rob Outman" wrote in message
news:50FDEA3DC08B093D54473B0C99F614FD@in.WebX.maYIadrTaRb...
Can you use the Format Function on a variable that is declared as an
integer? If not, how can I get an Integer variable to hold the
results of a string with the following code:

Rnum2 = txtStrtNum.Text 'in this case txtstrtnum.text = A001
Cnt = Mid$(Rnum2, 2, 4) 'Since the first two characters are zeros,
Cnt changes it too an integer? and Cnt now = "1" and I want it to =
"001"

How do I force Cnt to = "001"

Thanx,

Rob
0 Likes
Message 4 of 11

Anonymous
Not applicable
Hi guys,

Thanx for the help. Unfortunately I can not get them to work... at least
not all the way.

Frank,

Yes, the Rnum2 variable is a string and will always be (ANNN) I could not
get the following:

Cnt = Mid$(Rnum2, 2)

or

Cnt = Format(Mid$(Rnum2, 2), "000")

to equal "001" no matter what I tried. I started a brand new module and
tried it there too, no go. Am I missing something?

Mark,

I did get yours to work but only until it came across the next digit in the
loop. I wrote the following IF THEN statements:

If Cnt < 10 Then Rnum = Rnum & "00"
' The above works fine until it gets above 9 then it adds a "0" to the front
of the number and 'reads something like A0010

If Cnt = 10 - 99 Then Rnum = Rnum & "0" 'Is this a valid statement?

'The above works fine until it gets above 99 then it adds a "0" to the front
of the number and 'reads something like A0100

If Cnt > 100 Then Rnum = Rnum
'I can't get above 100 to work at all.

End If

I did try them the other way around (Then Rnum = "00" & Rnum) but this just
returns something like A1, A2, A3 etc...

Anymore suggestions would be greatly appreciated.

Thanx again guys,

Rob

"Frank Oquendo" wrote in message
news:BD0B3B45C22005D674959010D80E2357@in.WebX.maYIadrTaRb...
> Assuming Rnum2 is a String variable and always in the format ANNN
> (alpha, numeric, numeric, numeric), Mid$(Rnum2, 2) wil return "001"
> (in the case of "A001").
>
> If you're not sure about the value being properly zero-padded,
> Format(Mid$(Rnum2, 2), "000") will return "001", even if the original
> text was "A1"
>
> Hope this helps.
>
> --
> http://www.acadx.com
> Win a free autographed copy of
> "AutoCAD 2000 VBA Programmer's Reference"
> by Joe Sutphin
>
> "Rob Outman" wrote in message
> news:50FDEA3DC08B093D54473B0C99F614FD@in.WebX.maYIadrTaRb...
> Can you use the Format Function on a variable that is declared as an
> integer? If not, how can I get an Integer variable to hold the
> results of a string with the following code:
>
> Rnum2 = txtStrtNum.Text 'in this case txtstrtnum.text = A001
> Cnt = Mid$(Rnum2, 2, 4) 'Since the first two characters are zeros,
> Cnt changes it too an integer? and Cnt now = "1" and I want it to =
> "001"
>
> How do I force Cnt to = "001"
>
> Thanx,
>
> Rob
>
0 Likes
Message 5 of 11

Anonymous
Not applicable
Hi Rob,

In your last response to Frank, you state that the Rnum2 variable is a
string. I got the impression from your first post that Cnt was an integer.
Depending on what Cnt is declared as will affect the result. For example,

dim Cnt as Integer

Cnt = Format(Mid$(Rnum2, 2), "000")

will yield Cnt = 1 when Rnum2 = "A001"

However,

dim Cnt as String

Cnt = Format(Mid$(Rnum2, 2), "000")

will yield Cnt = "001" when Rnum2 = "A001"

Hope this helps,

David
0 Likes
Message 6 of 11

Anonymous
Not applicable
Ok here is some more clarification. I changed Cnt to a variable and it now
works is I add a breakpoint and view the result. But the only problem is
after the loop runs, the Cnt value changes from say "001" to "1". Is there
a statement I can add to the code in the loop that will force the Cnt
variable to retain the "001" value and then continue on?

Thanx,

Rob

"David Moroni" wrote in message
news:7F8D73ED5B650F68F80E742A7B5532E3@in.WebX.maYIadrTaRb...
> Hi Rob,
>
> In your last response to Frank, you state that the Rnum2 variable is a
> string. I got the impression from your first post that Cnt was an
integer.
> Depending on what Cnt is declared as will affect the result. For example,
>
> dim Cnt as Integer
>
> Cnt = Format(Mid$(Rnum2, 2), "000")
>
> will yield Cnt = 1 when Rnum2 = "A001"
>
> However,
>
> dim Cnt as String
>
> Cnt = Format(Mid$(Rnum2, 2), "000")
>
> will yield Cnt = "001" when Rnum2 = "A001"
>
> Hope this helps,
>
> David
>
0 Likes
Message 7 of 11

Anonymous
Not applicable
Hi Rob,

Your code has probably gone through several changes since you first
posted your question. Could you post your procedure with the latest
changes? This might help diagnose the problem.

David
0 Likes
Message 8 of 11

Anonymous
Not applicable
Hi David,

Here is the code as it is currently. Just to warn you... It's pretty long.
Basically, it goes through, has the user select a roomtag block, change the
attribute value to the textbox value the user entered, adds one to the Cnt
variable and then does the next one the user selects. The first tag works
fine, but after that the Cnt value changes (somehow) and then it leaves out
the necessary 0's (zeros). I can't figure this out. Any guesses?


Private Sub cmdRun_Click()

Dim fType, fData, ss As AcadSelectionSet
Dim varBlkattribs As Variant
Dim strBlkAttribs As String
Dim blkRef As AcadBlockReference
Dim objBlkSelect As AcadSelectionSet
Dim intarray() As Integer
Dim attarry As AcadAttribute
Dim Point(0 To 2) As Double
Dim strArray As String
Dim Acadblock As AcadBlockReference
Dim Ent As AcadEntity
Dim Pt As Variant
Dim Cnt As Variant 'or Integer (variant works better)
Dim rnum As String
Dim rnum2 As String
Dim Rnum3 As Variant
Dim endResult As String
Dim optcomp As String

'Set a filter to search for all instances of the "Roomtag" block and apply
the count to the ss 'variable.
With ThisDrawing.Utility
Set ss = CreateSelectionSet()
BuildFilter fType, fData, 0, "INSERT", 2, "ROOMTAG"
ss.Select acSelectionSetAll, , , fType, fData
'Prompt the user as to how many roomtags there are in the drawing.
.Prompt vbCr & "There are " & ss.Count & " Roomtags in this drawing."
End With

'hide the form
Me.Hide

'If the first letter in the textbox is an "A" then do the following
If Left(txtStrtNum.Text, 1) = LCase("A") Then

rnum2 = txtStrtNum.Text
Cnt = Format(Mid$(rnum2, 2), "000")
rnum = Mid$(rnum2, 1, 1)
endResult = UCase(rnum) & Cnt

On Error Resume Next
If ss.Count > 0 Then
Do
'Have the user select the tags in order and udate them as they are
selected.
ThisDrawing.Utility.GetEntity Ent, Pt, "Select the roomtags in
order: "
Ent.Highlight True
If Err Then
If ThisDrawing.GetVariable("errno") = "7" Then
Err.Clear
Else
Err.Clear
Exit Do
End If
End If
'check to see if the entity selected is a block
If TypeOf Ent Is AcadBlockReference Then
Set blkRef = Ent
'check to see if the block is named "Roomtag"
If blkRef.Name = "ROOMTAG" Then
'Check to see if the roomtag has attributes.
If blkRef.HasAttributes Then
varBlkattribs = blkRef.GetAttributes
varBlkattribs(0).TextString = endResult
End If
End If
'add 1 (one) to each roomtag as each one is selected.
Cnt = Cnt + 1
endResult = UCase(rnum & (Cnt))
Else
'If the entity selected is not a roomtag block then send the
following message.
MsgBox "The Object You Selected Is Not A Roomtag."
End If
Ent.Highlight False
Loop
End If
Ent.Highlight False

'If there is no value "A" in the textbox, then do the folowing:
'The folowing section works with no problems.
Else
Cnt = txtStrtNum.Text
End If

On Error Resume Next
If ss.Count > 0 Then
Do
'Have the user select the tags in order and udate them as they are
selected.
ThisDrawing.Utility.GetEntity Ent, Pt, "Select the roomtags in
order: "
Ent.Highlight True
If Err Then
If ThisDrawing.GetVariable("errno") = "7" Then
Err.Clear
Else
Err.Clear
Exit Do
End If
End If
'check to see if the entity selected is a block
If TypeOf Ent Is AcadBlockReference Then
Set blkRef = Ent
'check to see if the block is named "Roomtag"
If blkRef.Name = "ROOMTAG" Then
'Check to see if the roomtag has attributes.
If blkRef.HasAttributes Then
varBlkattribs = blkRef.GetAttributes
varBlkattribs(0).TextString = Cnt
End If
End If
'add 1 (one) to each roomtag as each one is selected.
Cnt = Cnt + 1
Else
'If the entity selected is not a roomtag block then send the
following message.
MsgBox "The Object You Selected Is Not A Roomtag."
End If
Ent.Highlight False
Loop
End If
Ent.Highlight False

End Sub

Thanx in advance for any suggestions you may have.

Rob

"David Moroni" wrote in message
news:396F057CD9704DA571DF6A05834A8E4D@in.WebX.maYIadrTaRb...
> Hi Rob,
>
> Your code has probably gone through several changes since you first
> posted your question. Could you post your procedure with the latest
> changes? This might help diagnose the problem.
>
> David
>
0 Likes
Message 9 of 11

Anonymous
Not applicable
Hi Rob,

Unfortunately, I have to leave town this afternoon, so I have just
glanced at the code you posted. However, I think the problem is how you are
handling the Cnt variable. Cnt is used with both string and numerical
operations. Here's my suggestion on how to acheive what you want. First,
declare Cnt as an Integer.

Dim Cnt as Integer

Then change

Cnt = Format(Mid$(rnum2, 2), "000")

to

Cnt = Val(Mid$(rnum2, 2))

then do your renumbering manipulation. When you are ready to build the
endResult variable, use a Select Case similar to

Select Case Cnt

Case 0 to 9

endResult = UCase(rnum) & "00" & Cnt

Case 10 to 99

endResult = UCase(rnum) & "0" & Cnt

Case 100 to 999

endResult = UCase(rnum) & Cnt

Case > 999

(Some error handling code here)

End Select

This way, Cnt is always referred to as an integer and the formatting of the
string variable is handled in the Select Case.

Hope this helps,

David
0 Likes
Message 10 of 11

Anonymous
Not applicable
Finally it works!!! Your suggestion to use a select case to build the
endResult clued me in. I had to build the endResult inside of the Do Loop.
It works perfectly now!!! There is one minor flaw that I am going to work
on to perfect it and then it will finally be done. This macro has been the
hardest one I have tried to create yet... but it has been worth it.

The good thing is that I still love this programming stuff!!!

Thanx for all of your help David. And thanx to everyone else for the
suggestions.

Rob

"David Moroni" wrote in message
news:26DD81DACA3679BC83F7AEA74ACC3234@in.WebX.maYIadrTaRb...
> Hi Rob,
>
> Unfortunately, I have to leave town this afternoon, so I have just
> glanced at the code you posted. However, I think the problem is how you
are
> handling the Cnt variable. Cnt is used with both string and numerical
> operations. Here's my suggestion on how to acheive what you want. First,
> declare Cnt as an Integer.
>
> Dim Cnt as Integer
>
> Then change
>
> Cnt = Format(Mid$(rnum2, 2), "000")
>
> to
>
> Cnt = Val(Mid$(rnum2, 2))
>
> then do your renumbering manipulation. When you are ready to build the
> endResult variable, use a Select Case similar to
>
> Select Case Cnt
>
> Case 0 to 9
>
> endResult = UCase(rnum) & "00" & Cnt
>
> Case 10 to 99
>
> endResult = UCase(rnum) & "0" & Cnt
>
> Case 100 to 999
>
> endResult = UCase(rnum) & Cnt
>
> Case > 999
>
> (Some error handling code here)
>
> End Select
>
> This way, Cnt is always referred to as an integer and the formatting of
the
> string variable is handled in the Select Case.
>
> Hope this helps,
>
> David
>
0 Likes
Message 11 of 11

Anonymous
Not applicable


| It works perfectly now!!! There is one minor flaw that I am going to work
| on to perfect it and then it will finally be done.

Perfection is so transient...
0 Likes