Visual Basic Variable Incrementing

June 29, 2005 at 09:53:28
Specs: Windows XP, P4, 512MB

Guys...

I'm beside myself trying to figure out why this variable (clickcounter) will not increment.

As an overview, this program reads from the serial port based on an OnComm comEvReceive event. The serial port is receiving wireless transmission from a small switch.

When it does so, I want to display it into three text boxes. One is for the data as received, one is for an ASCII conversion, and the last is for a HEX conversion.

I can do that no problem.

However, I would like to keep track of how many times this switch is pressed.

You would think it would be an easy chore...just incrementing a variable within the comEvReceive event everytime the event is called. I can't get it to increment. It increments the first time (from 0 to 1) and then refuses to after that.

Please offer any help you can. Code is attached below.

Private Sub Command2_Click()


End Sub

Private Sub Command1_Click()


End Sub

Private Sub Form_Load()

With MSComm1

.CommPort = 1
.Handshaking = 2 - comRTS
.RThreshold = 1
.RTSEnable = True
.Settings = "9600,n,8,1"
.SThreshold = 1
.PortOpen = True
.InputLen = 1

End With

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""

End Sub

Private Sub Form_Unload(Cancel As Integer)

MSComm1.PortOpen = False

End Sub

Private Sub MSComm1_OnComm()
Dim InBuffer As String
Dim AscData As String
Dim HexData As String
Dim ClickCounter As Integer

Select Case MSComm1.CommEvent
Case comEvReceive

InBuffer = MSComm1.Input

Text1.SelStart = Len(Text1.Text)
Text1.SelText = InBuffer

AscData = Asc(InBuffer)

Text2.SelStart = Len(Text2.Text)
Text2.SelText = AscData

HexData = Hex(AscData)

Text3.SelStart = Len(Text3.Text)
Text3.SelText = HexData

ClickCounter = ClickCounter + 1
Label1.Caption = ClickCounter


End Select

End Sub



See More: Visual Basic Variable Incrementing

Report •


#1
June 29, 2005 at 10:11:54

The problem is that you defining the variable inside the OnComm function function it is being used. When the function exits, the variable, as are all function wide variables, reset to zero.

Two ways round the problem. Define the variable outside the function in the General Declarations section of the form. This will make the variable available to all functions in that form and it will retain its value when as long as the form is loaded.

Seeing how the variable is only ever used inside that one function then define it as Static

Static ClickCounter As Integer

That way the variable will retain its value when the function exits.

There are numerous ways of defining a variable: Global, Static, Public and Private.

Without any instructions to the contrary, Dim on it own makes a variable Private and Dynamic within the context in which it is declared.

Stuart


Report •

#2
June 29, 2005 at 10:21:15

Thanks Stuart.

It's been forever since I've done any VB'ing....using VB 6.0 now. Most of my programming experience (3 semesters in college before switching majors) was in C++.

When I bring up the project code window, click the left menu bar to change it to (General) and the right bar to change to (Declarations), I get an error when I use the Static ClickCounter as Integer line.

It gives an error at compile time saying "Compile error: Invalid outside procedure."

When I change the very same line to "Dim ClickCounter as Integer", it compiles, but I still get the same problem as described in the first post.


Report •

#3
June 29, 2005 at 10:24:39

Alright...correction....The Dim ClickCounter as Integer line works. It does increment the variable. I still had it defined locally....hehehe....whoops...

Thanks again.


Report •

Related Solutions

#4
June 29, 2005 at 10:27:50

You have to define a Static variable inside a function. Just change Dim to Static in your original code.

Dim ClickCounter as Integer in the General Declarations section should work unless you are using the same variable name somewhere else in the module.

Stuart


Report •

#5
June 29, 2005 at 18:13:44

I am interested on how you are converting a switch closure wirelessly to the serial port. A parallel port or game port would be a more logical connection.

Report •

#6
July 25, 2005 at 05:59:10

Label1.Caption = Val(Label1.Caption) +1
that should help

Report •

#7
July 31, 2005 at 04:15:35

Hey guys

I am having a problem with incrementing an invoice number in Visual Basic 6. The number has to automatically increment everytime the user clicks the new button and everytime the form loads. The invoice number is in the format 00/00000 . Is there any way i can do this without linking to a database?

Terry


Report •

#8
July 31, 2005 at 04:53:35

Terry,

The normal convention is to start a new thread for a new question. Tagging you question onto the end of another thread means that there are only a two or three people reading it.

Stuart


Report •

#9
July 31, 2005 at 22:36:32

Thanks Stuart i'll do that.

Terry


Report •


Ask Question