Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple adding code. Not sure why I am getting a type mis match error

Posted on 2003-11-19
5
Medium Priority
?
334 Views
Last Modified: 2010-05-18
Have got a simple piece of code which keeps a running total of visits to companies. Have dimensioned the text box as integer but keep getting type mismatch error and dont understand why.

Dim july As Integer
Text8.Text = july

Data1.Recordset.Edit
july = july + 1
Data1.Recordset.Update

I have tried different combinations of this code. I either seem to get a type mismatch error or it simply just doesn't add.
0
Comment
Question by:matt_fennell
5 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9779916
all you are doing is incrementing the value of a variable, called july.  Editing the recordset does NOT, in any way, shape or form, cause the variable to be used in that recordset.


How does this code 'keep' a running total?

The way this code is wriiten, you create a varaibel, called july, which happens to be an Integer type (so when it is first created, it is assigned the default value 0).  You then assign the value of the variable july (which equals 0 at that point) to the Text of a TextBox control, named Text8.  SO now, Text8 displayes the value 0.

you then call the Edit method of the Recordset object associated with a DataControl, named Data1 (this does NOTHING to The variable 'july', or to the TextBox Text8).

You then Add 1 to the variable 'july' (so its value is now 1), but that also does NOTHING to the TextBox (which still shows the value 0).  This also does NOTHING to the Recordset, or the datacontrol for that matter. (The recordset has PRECISELY the same contents that it had BEFORE any of this code executed).  You then call the Update method of the Recordset (which would NORMALLY cause any CHANGES made to the recordset - but there HAVE BEEN NO CHANGES- to be written back to the table from which the recordset was originally retrieved).  But as I just said, there have been no changes, so NOTHING happens to the recordset, or to the table associated with the recordset.

What EXACTLY are you trying to do, with this code, and where is this being executed - in a CLICK event handler of a Button, for instance.

AW
0
 

Author Comment

by:matt_fennell
ID: 9780046
The code is stored in a command button. When clicked I wish for the text box to add one to its original number. In the case of starting from scratch, as the default value is 0, when the button is clicked I would like a one to appear and be text box and also be stored in the database.
0
 
LVL 10

Expert Comment

by:D_M_D
ID: 9781759
Here you go... This will properly increment july
Make july global

Dim july As Integer

Private Sub Command1_Click()

Data1.Recordset.Edit
july = july + 1
Data1.Recordset.Update

Text8.Text = july
End Sub

Private Sub Form_Load()
' on form load set the textbox
Text8.Text = july
End Sub

--------
D_M_D
0
 
LVL 8

Accepted Solution

by:
SNilsson earned 80 total points
ID: 9782339
Or better still use it as static:

Static july As Integer
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9783194
but even so, this block:

Data1.Recordset.Edit
july = july + 1
Data1.Recordset.Update

DOES NOTHING other than incrementing the varaible 'july'.  Nothing is ever saved, permanently.  

What do you THINK you are doing with the Recordset, in this code?

AW
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Starting up a Project
Screencast - Getting to Know the Pipeline

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question