Computing.Net > Forums > Programming > Visual Basic Variable Incrementing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Visual Basic Variable Incrementing

Reply to Message Icon

Name: blykins
Date: June 29, 2005 at 09:53:28 Pacific
OS: Windows XP
CPU/Ram: P4, 512MB
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: June 29, 2005 at 10:11:54 Pacific
Reply:

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


0

Response Number 2
Name: blykins
Date: June 29, 2005 at 10:21:15 Pacific
Reply:

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.


0

Response Number 3
Name: blykins
Date: June 29, 2005 at 10:24:39 Pacific
Reply:

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

Thanks again.


0

Response Number 4
Name: StuartS
Date: June 29, 2005 at 10:27:50 Pacific
Reply:

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


0

Response Number 5
Name: wizard-fred
Date: June 29, 2005 at 18:13:44 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: DeadDesigner
Date: July 25, 2005 at 05:59:10 Pacific
Reply:

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


0

Response Number 7
Name: terry_dz
Date: July 31, 2005 at 04:15:35 Pacific
Reply:

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


0

Response Number 8
Name: StuartS
Date: July 31, 2005 at 04:53:35 Pacific
Reply:

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


0

Response Number 9
Name: terry_dz
Date: July 31, 2005 at 22:36:32 Pacific
Reply:

Thanks Stuart i'll do that.

Terry


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Visual Basic Variable Incrementing

Visual Basic.NET increment variable www.computing.net/answers/programming/visual-basicnet-increment-variable/17031.html

Visual Basic 2005, write to file www.computing.net/answers/programming/visual-basic-2005-write-to-file/17181.html

Visual Basic Project Help Needed... www.computing.net/answers/programming/visual-basic-project-help-needed/17187.html