Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reset a variable

Status
Not open for further replies.

ApplePirate

Programmer
Dec 16, 2002
65
GB
how do i reset a variable? Never ever, bloody anything, ever
 
OK.. ive tried that and i dont know how the code is run.
so here what im using.

Option Compare Database
Option Explicit

Public RecordNum
Public myInt As Integer 'Public variable

Public Function GetSeq(strdummy As String) As Integer
myInt = myInt + 1
GetSeq = myInt
End Function

the problem is if the code is run once everything is fine as the variable begins at zero and recounts from 1 to 10. but if the code is run again the variable begins from the last number and everything is renumbered from 11 to 20.

i have a number of computer rabbits (they startle easily) using my database and i dont want to panic them. So how do i set the variable 'myInt' to zero so that every time this code is run it renumbers the records from 1 to X Never ever, bloody anything, ever
 
Public Function GetSeq(strdummy As String) As Integer
if myInt=10 then myInt=0
myInt = myInt + 1
GetSeq = myInt
End Function

hth

Ben ----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
This works and thanks very much...
however the number of records will increase, the above code only works up to 10 then an error window pops up. perhaps thats my fault for not being clear in earlier posts
is there a way of setting the variable to 0 on some sort of event. i.e. in the form onload event, if myInt>0 then myInt=0
Never ever, bloody anything, ever
 
What is it you are using the number for? This is astarting to sound very complicated!
You can do what you wanted, put the public variable in a module, then call myvar=0 on the onOpen event of whatever form you want!

Like I said though, we could do with more details to see if there is not a better way of doing it.

B ----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Its a delivery list, and i need to be able to slot a new customer into the delivery sequence. Never ever, bloody anything, ever
 
Not sure exactly what you are trying to accomplish, but in general, global or public variables are bad.

I still use them occasionally, but when I do I try to use a naming convention that is very obvious. All my global variable start with G_, like G_Delivery_Seq_no, for instance. Not usually a big deal for small projects. But, if you have a poor memory like me, it can be important several years down the road when you are trying to figure out what you did :)

Anyway, you could do this several ways, but here is one.

Function GetSeq(MaxSlot As Integer) As Integer
' Note: MaxSlot=0 resets slot to 0
Static intSlot As Integer

Select Case MaxSlot
Case 0
intSlot = 0
GetSeq = IntSlot
Case Else
GetSeq = intSlot
intSlot = intSlot + 1
If intSlot > MaxSlot Then intSlot = 0
End Select
End Function

You pass the max number of slots as a parameter to the function. I don’t know how you derive this, you could just keep a public variable somewhere with this info and use that.

IntSlot is declared ‘static’. This means it will maintain it’s value thru subsequent invocations of the function. I added a small enhancement, calling the function with MaxSlot=0 will reset intslot to 0.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top