visual basic simple calculator - visual-studio

I wrote simple calculator with visual basic but when I debug my code, it has problem and dose not run correctly .
my code
Public Sub general()
Dim num1 As Long, num2 As Long
Dim result As Single
Dim op As String
End Sub
Private Sub Form_Load()
Text1 = " "
num1 = "0"
num2 = "0"
op = " "
one.Caption = "1"
two.Caption = "2"
three.Caption = "3"
four.Caption = "4"
five.Caption = "5"
six.Caption = "6"
seven.Caption = "7"
eight.Caption = "8"
nine.Caption = "9"
zero.Caption = "0"
clear11.Caption = "cls"
Equal12.Caption = "="
plus.Caption = "+"
min14.Caption = "-"
multi15.Caption = "*"
div16.Caption = "/"
lnx17.Caption = "1/x"
power18.Caption = "x^2"
arc19.Caption = "aqr(x)"
exit20.Caption = "Exit"
End Sub
Private Sub one_Click()
Text1 = Text1 + "1"
End Sub
Private Sub two_Click()
Text1 = Text1 + "2"
End Sub
Private Sub three_Click()
Text1 = Text1 + "3"
End Sub
Private Sub four_Click()
Text1 = Text1 + "4"
End Sub
Private Sub five_Click()
Text1 = Text1 + "5"
End Sub
Private Sub six_Click()
Text1 = Text1 + "6"
End Sub
Private Sub seven_Click()
Text1 = Text1 + "7"
End Sub
Private Sub eight_Click()
Text1 = Text1 + "8"
End Sub
Private Sub nine_Click()
Text1 = Text1 + "9"
End Sub
Private Sub zero_Click()
Text1 = Text1 + "0"
End Sub
Private Sub clear11_click() 'cls button
test1 = ""
num1 = "0"
num2 = "0"
op = ""
End Sub
Private Sub Equal12_click() 'Equal button
num2 = Val(Text1)
Select Case op
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
Case "1/x"
result = 1 / num1
Case "x^2"
result = num1 ^ 2
'Case "aqr(x)"
'result = aqr(num1)
End Select
Text1 = Str(result)
op = ""
End Sub
Private Sub plus_click()
num1 = Val(Text1)
op = "+"
Text1 = ""
End Sub
Private Sub min14_click()
num1 = Val(Text1)
op = "-"
Text1 = ""
End Sub
Private Sub multi15_click()
num1 = Val(Text1)
op = "*"
Text1 = ""
End Sub
Private Sub div16_click()
num1 = Val(Text1)
op = "/"
Text1 = ""
End Sub
Private Sub squareroot19_click() 'sqr(x)button
num1 = Val(Text)
result = Sqr(num1)
Text1 -Str(result)
End Sub
when I debug it I find the problem is "op" variable that not set in correct way because when I click on equal button and the program go to select case part the value of "op" is empty.
can any one help me how to fix it?

I've run the following in a single function and it seems to work for me. I get 3 when running the code within the methods to click 1, click +, and click 2.
I basically put the output of this into a textbox on a windows form, which displayed 3 when I ran this code.
I did add the line Dim text1 As String, did you declare this elsewhere?
Dim num1 As Long, num2 As Long
Dim result As Single
Dim op As String
Dim text1 As String
text1 = " "
num1 = "0"
num2 = "0"
op = " "
text1 = text1 + "1"
num1 = Val(text1)
op = "+"
text1 = ""
text1 = text1 + "2"
num2 = Val(text1)
Select Case op
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
Case "1/x"
result = 1 / num1
Case "x^2"
result = num1 ^ 2
'Case "aqr(x)"
'result = aqr(num1)
End Select
text1 = Str(result)
op = ""

Declare your varibles outside the general method. This will make the methods available to get/set them. An alternative could be to pass the buttons value as a parameter, but the first part is the easier!

Related

Run time error 5:invalid procedure call or argument

Here is the code which I have written in vb6...it is showing error in text1.setfocus
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
End If
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
End Sub
Do not have this code in LostFocus, instead try to have it in the Validate event, there would be an cancel parameter to the event, if you set Cancel = True (means the cursor will not exit the control) you need not do setfocus
Try the following:
Private Sub Text1_Validate(Cancel As Boolean)
If IsNumeric(Text1.Text) = False Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Cancel = True
End If
End Sub
Try this if you have an empty string and you are trying to work with in in your loop you can get an invalid procedure call. skip over it all togehter don't run the loop if the text is empty.
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
else
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
endif
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
End Sub

How to capitalize first letter of a string in VBScript

I read a really helpful post here How do I make the first letter of a string uppercase in JavaScript? and I was curious of a good solution using VBScript.
>> s = "first word of string"
>> WScript.Echo UCase(Left(s, 1)) & Mid(s, 2)
>>
First word of string
str = "mY nAME iS sACHIN"
arrStr = split(str," ")
For i=0 to ubound(arrStr)
word = lcase(trim(arrStr(i)))
word = replace(word,mid(word,1,1),chr(asc(mid(word,1,1))-32),1,1)
str1 = str1 & word & " "
next
msgbox trim(str1)
Output: My Name Is Sachin
str = "mY nAME iS sACHIN"
arrStr = split(str," ")
For i=0 to ubound(arrStr)
word = lcase(trim(arrStr(i)))
word = replace(word,mid(word,1,1),ucase(mid(word,1,1)),1,1)
str1 = str1 & word & " "
next
msgbox trim(str1)

MSFlexGrid Edit VB6

Currently I am working on vb6 application. I want to show data in MSFlexgrid But there is no edit Facility in MSFlexgrid Control.
Is there Any way to Edit MSFlexgrid?
There is a way using hidden Textbox. On the double click on the cell the textbox will be visible and Edit is possible here is code snippet check it
Private Sub Form_Load()
'Setting Col And row
MSFlexGrid1.Cols = 3
MSFlexGrid1.Rows = 10
'First row
MSFlexGrid1.TextMatrix(0, 0) = "ID"
MSFlexGrid1.TextMatrix(0, 1) = "Date"
MSFlexGrid1.TextMatrix(0, 2) = "Voucher Type"
'some data
MSFlexGrid1.TextMatrix(1, 0) = "E0000001"
MSFlexGrid1.TextMatrix(2, 0) = "E0000001"
MSFlexGrid1.TextMatrix(1, 1) = "01/04/10"
MSFlexGrid1.TextMatrix(2, 1) = "01/04/10"
MSFlexGrid1.TextMatrix(1, 2) = "Jrnl"
MSFlexGrid1.TextMatrix(2, 2) = "Jrnl"
End Sub
Private Sub MSFlexGrid1_DblClick()
'If MSFlexGrid1.Col = 3 Or MSFlexGrid1.Col = 6 Or MSFlexGrid1.Col = 7 Then
GridEdit Asc(" ")
'End If
End Sub
Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
GridEdit KeyAscii
End Sub
Sub GridEdit(KeyAscii As Integer)
'use correct font
Text1.FontName = MSFlexGrid1.FontName
Text1.FontSize = MSFlexGrid1.FontSize
Select Case KeyAscii
Case 0 To Asc(" ")
Text1 = MSFlexGrid1
Text1.text = Trim(Text1.text)
Text1.SelStart = 1000
Case Else
Text1 = MSFlexGrid1
Text1.text = Trim(Text1.text)
Text1.SelStart = 1000
End Select
'position the edit box
Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
Text1.Width = MSFlexGrid1.CellWidth
Text1.Height = MSFlexGrid1.CellHeight
Text1.Visible = True
Text1.SetFocus
End Sub
Private Sub MSFlexGrid1_LeaveCell()
If Text1.Visible Then
If MSFlexGrid1.Col = 6 Or MSFlexGrid1.Col = 7 Then
If Text1.text = "" Then
Text1.text = " "
End If
End If
MSFlexGrid1 = Text1
Text1.Visible = False
End If
End Sub
Private Sub MSFlexGrid1_GotFocus()
If Text1.Visible Then
If MSFlexGrid1.Col = 6 Or MSFlexGrid1.Col = 7 Then
If Text1.text = "" Then
Text1.text = " "
End If
End If
MSFlexGrid1 = Text1.text
Text1.Visible = False
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'noise suppression
If MSFlexGrid1.Col <> 6 And MSFlexGrid1.Col <> 7 Then
KeyAscii = 0
End If
If KeyAscii = vbKeyReturn Then
KeyAscii = 0
End If
End Sub
Can you also try below code. This doesnt require text box.
'Put this code in MSFlexGrid Keypress Event
'===================================================
Private Sub MSFlexGrid_KeyPress(KeyAscii As Integer)
With MSFlexGrid
Select Case KeyAscii
Case 8: 'IF KEY IS BACKSPACE THEN
If .Text <> "" Then .Text = _
Left$(.Text, (Len(.Text) - 1))
Case 13: 'IF KEY IS ENTER THEN
Select Case .Col
Case Is < (.Cols - 1):
SendKeys "{right}"
Case (.Cols - 1):
If (.Row + 1) = .Rows Then
.Rows = .Rows + 1
End If
SendKeys "{home}" + "{down}"
End Select
Case Else
.Text = .Text + Chr$(KeyAscii)
'write your own keyascii Validations under
'commented lines
Select Case .Col
Case 0, 1, 2:
'if (your condition(s)) then
'accept only charectors
'Else
' keyascii=0
'End If
Case Else:
End Select
End Select
End With
End Sub

VB6 Calculator: Show operator sign on the screen

I just started learning VB6 on my own. I have created a simple calculator and I would like it to display the "operator" on the screen.
For example, if I press "1", followed by "plus sign", then finally "8", I would like the calculator to show "1 + 8". And when the "equal" sign is pressed, the calculator should show "1 + 8 = 9".
Below is a very noob code I made:
Dim formula As String
Dim itemOne As Integer
Dim itemTwo As Integer
Private Sub btn1_Click()
txtboxScreen.Text = txtboxScreen.Text & 1
End Sub
Private Sub btn2_Click()
txtboxScreen.Text = txtboxScreen.Text & 2
End Sub
Private Sub btn3_Click()
txtboxScreen.Text = txtboxScreen.Text & 3
End Sub
Private Sub btn4_Click()
txtboxScreen.Text = txtboxScreen.Text & 4
End Sub
Private Sub btn5_Click()
txtboxScreen.Text = txtboxScreen.Text & 5
End Sub
Private Sub btn6_Click()
txtboxScreen.Text = txtboxScreen.Text & 6
End Sub
Private Sub btn7_Click()
txtboxScreen.Text = txtboxScreen.Text & 7
End Sub
Private Sub btn8_Click()
txtboxScreen.Text = txtboxScreen.Text & 8
End Sub
Private Sub btn9_Click()
txtboxScreen.Text = txtboxScreen.Text & 9
End Sub
Private Sub btnDivide_Click()
itemOne = txtboxScreen.Text
txtboxScreen.Text = ""
formula = "/"
End Sub
Private Sub btnEqual_Click()
itemTwo = txtboxScreen.Text
If formula = "+" Then
txtboxScreen.Text = itemOne + itemTwo
ElseIf formula = "-" Then
txtboxScreen.Text = itemOne - itemTwo
ElseIf formula = "*" Then
txtboxScreen.Text = itemOne * itemTwo
ElseIf formula = "/" Then
txtboxScreen.Text = itemOne / itemTwo
End If
End Sub
Private Sub btnMinus_Click()
itemOne = txtboxScreen.Text
txtboxScreen.Text = ""
formula = "-"
End Sub
Private Sub btnPlus_Click()
itemOne = txtboxScreen.Text
txtboxScreen.Text = ""
formula = "+"
End Sub
Private Sub btnTimes_Click()
itemOne = txtboxScreen.Text
txtboxScreen.Text = ""
formula = "*"
End Sub
Private Sub btnZero_Click()
txtboxScreen.Text = txtboxScreen.Text & 0
End Sub
You may want to think about using a control array for you number buttons. This will vastly simplify your code in this instance and especially for more complex projects:
Private formula As String
Private itemOne As Integer
Private itemTwo As Integer
Private Sub btnNumbers_Click(Index As Integer)
txtboxScreen.Text = txtboxScreen.Text & Index
End Sub
''Remainder of your code goes here
Also, when you are declaring variables in the Declaration section of the form you should use Private instead of Dim.
I think you would like to concatenate the operator sign of the pressed button with the value you had in the textbox on button_click event.
something like:
Private Sub btnPlus_Click()
txtboxScreen.Text = txtboxScreen.Text & " + "
End Sub
and on equal button, you'd like to evaluate the expression
Private Sub btnEqual_Click()
txtboxScreen.Text = txtboxScreen.Text & " = " & Eval(txtboxScreen.Text)
End Sub
evaluating using Eval() is not a robust solution, but it's a simple way to achive that functionality.
You should save your first number, second number and operator (you called it "formula") separately and handle setting the text of the text box separately. Here is one way to do it:
Dim formula As String
Dim itemOne As String 'This time this is string
Dim itemTwo As String 'This time this is string
Dim currentItem As String 'Will hold the current number being entered
Dim Result As String 'This is string, too.
All your buttons will have code like:
Private Sub btn1_Click()
currentItem = currentItem & "1"
UpdateText()
End Sub
The operator buttons:
Private Sub btnPlus_Click()
itemOne = currentItem
formula = "+"
UpdateText()
End Sub
And the Equals button:
Private Sub btnEqual_Click()
itemTwo = currentItem
If formula = "+" Then
'Str is optional, but Val's are necessary since
'itemOne and itemTwo are strings.
Result = Str( Val(itemOne) + Val(itemTwo) )
ElseIf ...
.
.
.
End If
UpdateText()
End Sub
Well, noticed the call to UpdateText() at the end of every subprocedure? Here it is:
Private Sub UpdateText()
txtboxScreen.Text = itemOne & formula & itemTwo
'If result is not empty, we will add the '=' part too
If Result <> "" Then
txtboxScreen.Text = txtboxScreen.Text & "=" & Result
End If
End Sub
You might also be interested in an AC/ON key which sets all the variables to "".
The method was not so neat, but it's the best thing you can do without an expression evaluator. An expression evaluator can calculate the entire formula as it is. Eval is such a function and you can find some implementations of it on the net.

randomly generate 40 questions in vb6? please help me

i've been trying to create a vb6 code that will randomize 10 questions but it's not working. i use sql as my database
here's my code:
Private Sub cmdNext_Click()
Dim real_ans As String
Dim nCnt As Integer
'nCnt = nCnt + 2
'Label3.Caption = nCnt
real_ans = Adodc1.Recordset.Fields("answer")
With Adodc2.Recordset
Dim grade As String
If (real_ans = "A" And Option1.Value = True) Or (real_ans = "B" And Option2.Value = True) Or (real_ans = "C" And Option3.Value = True) Or (real_ans = "D" And Option4.Value = True) Then
.Fields("english_score").Value = .Fields("english_score").Value + 1
nEnglish_score = Adodc2.Recordset.Fields("english_score").Value
Select Case nEnglish_score
Case Is >= 7 'If score above 80...
grade = "genius" 'Give an A
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 6 'If score above 70...
grade = "Superior " 'Give a B
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 5 'If score above 60...
grade = "High Average" 'Give a C
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 4 'If score above 50...
grade = "Average" 'Give a D
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 3 'If score above 40...
grade = "Low Average " 'Give a E
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 2 'If score above 30...
grade = "Moron" 'Give a F
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 1 'If score above 20...
grade = "Idiot " 'Give a G
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Else 'Else
grade = "xxxx" 'Give a G
With Adodc2.Recordset
.Fields("IQ").Value = grade
.Update
End With
End Select
End If
Adodc1.Recordset.MoveNext
End With
End Sub
Private Sub Command1_Click()
frmExam2.Show
frmExam2.SetFocus
End Sub
Private Sub Form_Initialize()
Command1.Enabled = False
With Adodc2.Recordset
.AddNew
.Fields("name").Value = cStudent_Name
.Fields("section").Value = cStudent_Section
.Fields("english_score").Value = "0"
.Fields("math_score").Value = "0"
.Fields("abstract").Value = "0"
.Fields("total_average").Value = "0"
.Fields("IQ").Value = "0"
.Update
End With
End Sub
Private Sub Text1_Change()
If Text1.Text <> "" Then
cmdNext.Enabled = True
Else
Command1.Enabled = True
cmdNext.Enabled = False
End If
End Sub
Please help me i cant figure out whats wrong.
yep what's the error message?
you don't save any space with this 'with':
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
you can remove that block of code to the end (after End Select), since its in every Case statement anyway.
So compress it down to:
Adodc2.Recordset.Fields("IQ").Value = grade
why not have your option values and answers stored in the same way so you can just do a single comparison, like:
if real_ans = OptionValue then ...
for random numbers, try this:
Function Rand(max As Long, Optional Min As Long) As Long
Dim s As Single
s = Rnd(1) * (max - Min + 1) + Min - 0.5
Rand = CLng(Round(s, 0))
End Function
and put RANDOMIZE in your form_load

Resources