View Full Version : VB6 testing
Writing a VB program now for my father for selling a chemical that kills vegetation (for around farm yards).
There are two kind--blue box and black box. I have a command button with an up arrow and one with a down arrow for each of them and a text box for each. Each text box has a zero in it at the begining. I wan't a 1 to be added to the text box each time the up arrow is clicked and the opposite for the down arrow. I am not sure how to code this one.
I think that in the text boxes have to be text = text and then the up-arrows have to be text = text + 1 and text = text - 1. That doesn't work but I think that it should be similar.
Paul Komski
07-25-2002, 10:20 PM
Two ideas to play with.
Also try with .value and no property at all (ie default chosen) instead of .text property
''''''Method A
Sub ButtonUp_click
If NumericBad(TextBoxA.text) Then Exit Sub
TextBoxA = UpDownByOne(TextBoxA.text, 1)
End Sub
Sub ButtonDown_click
If NumericBad(TextBoxA.text) Then Exit Sub
TextBoxA = UpDownByOne(TextBoxA.text, -1)
End Sub
Function UpDownByOne(intSent as integer, intIncrement as ingeger)
UpDownByOne = intSent + intIncrement
End Function
Function NumericBad(varSent as variant)
If Not IsNumeric(varSent) Then
NumericBad = True
MsgBox "A numeric value must be in the box"
Else
NumericBad = False
End if
End Function
''''''Method B
Sub ButtonX_Click
Dim intTemp as integer
intTemp = TextBoxA.text
intTemp = intTemp + 1 'change sign as required
TextBoxA = intTemp
End Sub
Make sure that the textbox has a numerical value (zero is fine) before running these or you will get errors if it has a null or string value.
To be honest with you, method A doesn't even make sense to me but method b does. I would like to go with that one. Here is what I was trying:
Private Sub Command1_Click()
Dim text As String
text = text + 1
End Sub
I think that it needs to be:
Private Sub Command1_Click()
Dim text As Integer
text = text
text = text + 1
End Sub
This still wont work, when i put a text = text for the coding for the text box or nothing at all.
Paul Komski
07-26-2002, 04:40 PM
I hope the notes explain why your code does nothing.
'YOU HAVE BEEN TRYING
Private Sub Command1_Click()
Dim text As Integer 'Declares variable called text but gives it no value so it is empty
text = text 'Makes this variable text = itself (ie still empty)
text = text + 1 'Adds one to the variable text (adds one to empty)
End Sub
'TRY THIS
Private Sub Command1_Click()
Dim text As Integer 'Declares variable called text but gives it no value so it is empty
text = TextBoxA.text 'gives the text variable the current non-updated value in the TextBoxA
text = text + 1 'Adds one to the variable text
TextBoxA = text 'sets the value of the variable text in TextBoxA
End Sub
You seem to be confusing a variable called text and a textbox called something like textbox. It helps to give your variables more meaningful names; for example:-
Private Sub Command1_Click()
Dim DataForChanging As Integer
If not IsNumeric(TextBoxA.text) then 'Prevent errors by allowing to exit sub with a message
Exit Sub
MsgBox "There must be a numeric value in the box"
End if
DataForChanging = TextBoxA.text
DataForChanging = DataForChanging + 1
TextBoxA = DataForChanging
End Sub
netharam
07-26-2002, 04:57 PM
try this one. This works fine.
***********
Dim cnt As Integer
Private Sub Command1_Click()
cnt = cnt + 1
Text1.Text = cnt
End Sub
Private Sub Command2_Click()
If (cnt > 0) Then
cnt = cnt - 1
Text1.Text = cnt
End If
End Sub
Private Sub Form_Load()
cnt = 0
Text1.Text = cnt
End Sub
****************
Is this Ok?:p
Paul Komski
07-26-2002, 05:08 PM
That looks fine. There are many ways of skinning a cat and some are more elegant than others. In this example you have declared a global variable rather than in the above examples where the variable exists only within the procedures. It all depends on what you want to achieve. ;)
-----------------------------------------------------------------
'TRY THIS
Private Sub Command1_Click()
Dim text As Integer 'Declares variable called text but gives it no value so it is empty
text = TextBoxA.text 'gives the text variable the current non-updated value in the TextBoxA
text = text + 1 'Adds one to the variable text
TextBoxA = text 'sets the value of the variable text in TextBoxA
End Sub
---------------------------------------------------------------------
Private Sub Command1_Click()
Dim number As Integer
number = 0
text1 = number + 1
End Sub
' that works, you were right, i was confusing the textbox with the text variable. your notes really helped me understand why that would not work
I have the number centered horizontally in the text box but how do I center it vertically?
Paul Komski
07-26-2002, 07:43 PM
I don't think there is (or is an easy) way to vertically align the text in a text box. The main use of them is for a user to enter text into them - (though they can be locked and just used to display data) - and it would be unusual to start entering text half way down a blank space.
However, you could "cheat" to get the desired effect. Just create a label on your form and place your text box in the middle of it. Set the label's caption to nothing (in effect an empty string "") and make sure the label is sent behind the text box. Adjust the label and the text boxes properties so that the borders, background and 3D effects are as you desire.
Ok, I'll try that.
Here's the next conundrum: each button will reset number to 0 so you can't really use the arrow buttons to move the number up or down. I have tried a if - then statement (if number = "" then number = 0 else text1=number + 1). Thought that would fix it. Why doesn't that work?
My code:
Private Sub Command1_Click()
Dim number As Integer
number = 0
Text1 = number + 1
End Sub
Private Sub Command2_Click()
Dim number As Integer
number = 0
Text1 = number - 1
End Sub
One won't allow the other to work. I don't know how to do it any other way but I know that it can be done, I have seen it before.
Paul Komski
07-26-2002, 08:23 PM
You could do this in the way Netharam posted by using a global variable set to 0 on loading the form. If you do this you might also want to have a reset button that will allow you to reset the value to 0 (or any other value for that mattter).
In your last example it goes back to zero each time you run the sub procedure because this is what your code tells it to do (number = 0), you need to modify this so that when you click on the command button the number variable is initially set to the current value in the text box. So instead of number = 0 it should be number = Text1.text If that doesn't work try number = Text1.value or just number = Text1 with no .property at all.
Dim number As Integer
Private Sub Command1_Click()
Dim number As Integer
Text1 = number + 1
End Sub
Private Sub Command2_Click()
Dim number As Integer
Text1 = number - 1
End Sub
This is what I have now. When I click command1, the textbox displays a 1 (as it should) but then when I click command2, the textbox displays a -1. Isn't 1 - 1 = 0?
...I see my problem but don't know how to put the number = 0 as a public/global constant.
Paul Komski
07-29-2002, 02:37 PM
You seem to be declaring the same NAME of a variable both globally and within each procedure. Having declared the variable globally there is no need to redeclare it within the procedures; unless, that is, you wish to reset it from scratch in that procedure. A variable declared within a procedure is reset each time the procedure is called and over-rides a global variable that "happens" to have the same name.
Or put another way; when you operate on a variable in a procedure, the program will look first inside it for the variable and only if it doesn't find it there will it look globally for a variable with that name.
If you do declare it "within" a procedure then give it a value, (0 for example), before you do anything else with it.
Dim number As Integer
Private Sub Command1_Click()
Text1 = number + 1
End Sub
Private Sub Command2_Click()
Text1 = number - 1
End Sub
Alright, the dimention statement sets the number variable to and integer, so that is not the problem. There is no coding in text1 but the caption is set to be 0.
I want the text box to display a 0 and the up and down command buttons to be able to put the number in the textbox to higher and lower values.
Paul Komski
07-29-2002, 06:43 PM
You're gradually coming round to Netharam's solution; so why not use it. If you re-read it you will see that in each Click Procedure he first updates the value of the variable and THEN sets textbox.text to equal the updated value of the variable. In addition he has a Form_Load() procedure which sets the value of the variable to 0 (when the form is first opened and loaded) and uses it to set the initial value of textbox.text to 0 for starting-up. He also included an if statement in the decreasing button's procedure so that textbox.text cannot show a negative number.
I was just trying to understand netherarams post. It works now.
I have a question about another VB project: I have VB6.0 for Dummies and have done some reading and have determined that I need to print a form using the Printer Object. The instructions given are quite cryptic. It says to set your margins (printer.scaletip = TopValue...) but where do you put tho coding for this?
I tried puting it in the button that I use to print the form, but something is wrong:
Private Sub Command4_Click()
Printer.ScaleTop = TopValue
Printer.scalebottom = BottomValue
Printer.ScaleLeft = LeftValue
invoice.PrintForm = RightValue
invoice.PrintForm
End Sub
I need to use this rather than just the invoice.PrintForm because my Father wants to use this program on his buisness paper with the letter-head and I have to put this about 2 inches down the page.
Paul Komski
07-29-2002, 10:02 PM
Wouldn't it be easier to just "Select All" on your form and move everything down 2 inches?
I tried that, but the two inches of the form are still there so the printer leaves that grey stuff in place of the rest of the form.
Here is another part of that same project:
Private Sub Command9_Click()
discount1 = Text14 + Text13
Discount 2 = discount1 + Text17
If Text16 > "" Then Discount3 = Text16 / 100
Discount = discount1 * Discount3
Text15 = Discount1 - Discount
End Sub
If text16 contains a number, it would divide it by 100 and then multiply it with the sum of textboxes 14,13 and 17 and then display the resulting number in text15.
Example: text14 (subtotal) contains 1000, text13 (frieght) contains 0 and text17 (GST) is also 0. If text16 is 10 then it is divided by 100 to get .1 that is multiplied by the sum of text14, 13 and 17 (1000) and then put into a variable so it is 100. The total(1000) - the 10% variable = the total(900).
But, when I click on command9, it says "Command or Function Not Defined"
HeadachesAbound
07-30-2002, 02:19 PM
Try doing your button calculations like this:
Private Sub Command1_Click()
txtBox1=txtBox1+1
End Sub
Private Sub Command2_Click()
if txtBox1>0 then
txtBox1=txtBox1-1
end if
End Sub
Just tested it and it works as expected. Also should create less overhead since you don't declare any additional variables.
--------------------------------------------------
When you printing, you have to account for the default printer margins, and then adjust to your print area. Try this:
---------------------------
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY = 90
Private Const PHYSICALOFFSETX = 112
Private Const PHYSICALOFFSETY = 113
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Sub Command1_Click()
Dim OffsetLeft As Long
Dim OffsetTop As Long
'Retrieve Left Margin
OffsetLeft = (GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX) / GetDeviceCaps(Printer.hdc, LOGPIXELSX)) * 1440
'Retrieve Top Margin
OffsetTop = (GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY) / GetDeviceCaps(Printer.hdc, LOGPIXELSY)) * 1440
'By default the printer measures units in twips, lets convert our offsets to twips
OffsetLeft = tpX(OffsetLeft, Printer.hdc)
OffsetTop = tpY(OffsetTop, Printer.hdc)
'Move to our new coordinates so that we can begin printing.
'In your case this would be 1 inch from the left and 2 inches from the top (in twips)
Printer.CurrentX = (1440 * 1) - OffsetLeft
Printer.CurrentY = (1440 * 2) - OffsetTop
'Begin your printing here. This should get you the results you need.
End Sub
Public Function tpY(lngTwips As Long, hdc As Long) As Long
Dim ppY As Long
ppY = GetDeviceCaps(hdc, LOGPIXELSY)
tpY = (lngTwips / 1440) * ppY
End Function
Public Function tpX(lngTwips As Long, hdc As Long) As Long
Dim ppX As Long
ppX = GetDeviceCaps(hdc, LOGPIXELSX)
tpX = (lngTwips / 1440) * ppX
End Function
---------------------------
Please note some of these lines wrapped. Make sure you unwrap them in your code.
-----Note:
I just tried this using the PrintForm Method. This method ignores the margins and currentx/currenty settings when it prints. You may need to send the information to the printer directly using other commands like Print, Line, or Circle. If what you are trying to print is guaranteed to be same format each time then this may be your best option.
Why not declare extra variable?
Here is what I have in command button 4.
Private Sub Command4_Click()
Printer.ScaleTop = TopValue
Printer.scalebottom = BottomValue
Printer.ScaleLeft = LeftValue
invoice.PrintForm = RightValue
invoice.PrintForm
End Sub
This dosn't work. What do I have to do to it.
I was just using printform.invoce but that causes it to pring right over the letter-head on the paper. I need to move it about 2 inched down and I think that using the Printer Object would be the only way.
I want to print a form on a paper that has a letter-head so it needs to be 2-3 cm. from the top of the page. As far as I know, the only way to do that is with the printer object.
I have done some experimenting with that and have the coding:
Private Sub Command4_Click()
Printer.ScaleTop = 200
Printer.ScaleBottom = 10
Printer.ScaleLeft = 10
invoice.PrintForm = 10
End Sub
But when I try to compile the program into an .exe file, there is a compile error. When I delete the above code, there is no compile error.
vBulletin v3.6.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.