JeffreyRS
12-08-2008, 10:08 PM
Hi everyone, I am relatively new to vba coding, but i have had experience with coding in other languages, so I'm not completely ignorant. I have been coding a program to run an the ideal gas law, solving for both pressure and volume.
Here is the main code (module 1), (also note that i have visual basic 6.5, version 1024):
Public t As Double
Public n As Double
Public p As Double
Public r As Double
Public v As Double
Public units As String
Function PressCalc()
n = PressureCalc.MoleInputP.Text
v = PressureCalc.VolumeInputP.Text
t = PressureCalc.TempInputP.Text
If v = 0 Then
ErrorBoxPressure.Show
Else
Select Case PressureCalc.SIUnitsP
Case PressureCalc.SIUnitsP.Value = True
r = 8.314 ' in terms of m^3*Pa/g-mol*K
p = n * r * t / v
units = Pa
DispAnsP.AnswerP.Caption = MainCode.p
DispAnsP.UnitsP.Caption = MainCode.units
Case Else
r = 10.73516 ' in terms of psia*ft^3/lb-mol*degrees R
p = n * r * t / v
units = psia
DispAnsP.AnswerP.Caption = MainCode.p
DispAnsP.UnitsP.Caption = MainCode.units
End Select
DispAnsP.Show
Unload PressureCalc
End If
End Function
Function VolCalc()
t = volumecalc.TempInput.Text
n = volumecalc.MoleInput.Text
p = volumecalc.PressureInput.Text
If p = 0 Then
ErrorBoxVolume.Show
Else
Select Case volumecalc.SIUnitsV
Case volumecalc.SIUnitsV.Value = True
r = 8.314 ' in terms of m^3*Pa/g-mol*K
v = n * r * t / p
units = m ^ 3
DispAnsV.AnswerV.Caption = MainCode.v
DispAnV.UnitsV.Caption = MainCode.units
Case Else
r = 10.73516 ' in terms of psia*ft^3/lb-mol*degrees R
v = n * r * t / p
units = ft ^ 3
DispAnsV.AnswerV.Caption = MainCode.v
DispAnsV.UnitsV.Caption = MainCode.units
End Select
DispAnsV.Show
End If
End Function
Function clipboard() ' copies the answer to the clipboard
Dim copytoboard As DataObject
Set copytoboard = New DataObject
copytoboard.SetText "MainCode.p"
copytoboard.PutInClipboard
End Function
To give everyone a better feel for how the program is designed, the first userform asks the user to calculate either pressure or volume, where he or she inputs the values (t, n, p or v) and then clicks calculate, which runs a function I've defined (presscalc, volcalc). Then its sends the user to a display answer userform.
I've been primarily been getting a "runtime error 13; type mismatch," which leads me to believe that I have falsely defined some variables, but they all defined correctly, to me at least.
Also, when I do get an answer, if when i click the exit program button on the display answer userform, i get a "runtime error 424; object required." It makes no sense to me because the only coding on that userform tells it to unload the program. Here is the code anyways:
Private Sub CancelP_Click()
Unload DispAnsP
PressureCalc.Show
End Sub
Private Sub ExitP_Click()
Unload DispAnsP
End Sub
I'll also include the code for one of the input userforms, just to be complete:
Private Sub AmericanUnitsP_Click()
TempLabelP.Caption = "Temperature (?R)"
MoleLabelP.Caption = "Moles (lb-mol)"
VolumeLabelP.Caption = "Volume (ft^3)"
End Sub
Private Sub CalculatePC_Click()
Unload PressureCalc
End Sub
Private Sub CancelPC_Click()
Unload PressureCalc
Choice.Show
End Sub
Private Sub CalculateP_Click()
PressCalc.Run
Unload PressureCalc
End Sub
Private Sub ExitPC_Click()
Unload PressureCalc
End Sub
Private Sub SIUnitsP_Click()
TempLabelP.Caption = "Temperature (K)"
MoleLabelP.Caption = "Moles (g-mol)"
VolumeLabelP.Caption = "Volume (m^3)"
End Sub
''This code prevents letters from being inputted into the textbox
Private Sub MoleInputP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.MoleInputP.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub UserForm_Click()
End Sub
''This code prevents letters from being inputted into the textbox
Private Sub VolumeInput_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.VolumeInput.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
''This code prevents letters from being inputted into the textbox
Private Sub TempInputP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TempInputP.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Also note that I've included a toggle button to switch between SI and American Engineering Units, which works in the input screen, but won't calculate (yet another bug I don't get)
To top it off, the units string I've defined won't display in the final display box either. I have no idea why.
If anyone could help debug this biatch, words couldn't describe how grateful I would be; I don't want to think about the time I have put into looking over each line, only to think that the program should run flawlessly.
Again thank you everyone for considering my conundrum :hi:
PS Also, I would be happy to email this file if anyone wants to take a further look at it.
Here is the main code (module 1), (also note that i have visual basic 6.5, version 1024):
Public t As Double
Public n As Double
Public p As Double
Public r As Double
Public v As Double
Public units As String
Function PressCalc()
n = PressureCalc.MoleInputP.Text
v = PressureCalc.VolumeInputP.Text
t = PressureCalc.TempInputP.Text
If v = 0 Then
ErrorBoxPressure.Show
Else
Select Case PressureCalc.SIUnitsP
Case PressureCalc.SIUnitsP.Value = True
r = 8.314 ' in terms of m^3*Pa/g-mol*K
p = n * r * t / v
units = Pa
DispAnsP.AnswerP.Caption = MainCode.p
DispAnsP.UnitsP.Caption = MainCode.units
Case Else
r = 10.73516 ' in terms of psia*ft^3/lb-mol*degrees R
p = n * r * t / v
units = psia
DispAnsP.AnswerP.Caption = MainCode.p
DispAnsP.UnitsP.Caption = MainCode.units
End Select
DispAnsP.Show
Unload PressureCalc
End If
End Function
Function VolCalc()
t = volumecalc.TempInput.Text
n = volumecalc.MoleInput.Text
p = volumecalc.PressureInput.Text
If p = 0 Then
ErrorBoxVolume.Show
Else
Select Case volumecalc.SIUnitsV
Case volumecalc.SIUnitsV.Value = True
r = 8.314 ' in terms of m^3*Pa/g-mol*K
v = n * r * t / p
units = m ^ 3
DispAnsV.AnswerV.Caption = MainCode.v
DispAnV.UnitsV.Caption = MainCode.units
Case Else
r = 10.73516 ' in terms of psia*ft^3/lb-mol*degrees R
v = n * r * t / p
units = ft ^ 3
DispAnsV.AnswerV.Caption = MainCode.v
DispAnsV.UnitsV.Caption = MainCode.units
End Select
DispAnsV.Show
End If
End Function
Function clipboard() ' copies the answer to the clipboard
Dim copytoboard As DataObject
Set copytoboard = New DataObject
copytoboard.SetText "MainCode.p"
copytoboard.PutInClipboard
End Function
To give everyone a better feel for how the program is designed, the first userform asks the user to calculate either pressure or volume, where he or she inputs the values (t, n, p or v) and then clicks calculate, which runs a function I've defined (presscalc, volcalc). Then its sends the user to a display answer userform.
I've been primarily been getting a "runtime error 13; type mismatch," which leads me to believe that I have falsely defined some variables, but they all defined correctly, to me at least.
Also, when I do get an answer, if when i click the exit program button on the display answer userform, i get a "runtime error 424; object required." It makes no sense to me because the only coding on that userform tells it to unload the program. Here is the code anyways:
Private Sub CancelP_Click()
Unload DispAnsP
PressureCalc.Show
End Sub
Private Sub ExitP_Click()
Unload DispAnsP
End Sub
I'll also include the code for one of the input userforms, just to be complete:
Private Sub AmericanUnitsP_Click()
TempLabelP.Caption = "Temperature (?R)"
MoleLabelP.Caption = "Moles (lb-mol)"
VolumeLabelP.Caption = "Volume (ft^3)"
End Sub
Private Sub CalculatePC_Click()
Unload PressureCalc
End Sub
Private Sub CancelPC_Click()
Unload PressureCalc
Choice.Show
End Sub
Private Sub CalculateP_Click()
PressCalc.Run
Unload PressureCalc
End Sub
Private Sub ExitPC_Click()
Unload PressureCalc
End Sub
Private Sub SIUnitsP_Click()
TempLabelP.Caption = "Temperature (K)"
MoleLabelP.Caption = "Moles (g-mol)"
VolumeLabelP.Caption = "Volume (m^3)"
End Sub
''This code prevents letters from being inputted into the textbox
Private Sub MoleInputP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.MoleInputP.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub UserForm_Click()
End Sub
''This code prevents letters from being inputted into the textbox
Private Sub VolumeInput_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.VolumeInput.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
''This code prevents letters from being inputted into the textbox
Private Sub TempInputP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TempInputP.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Also note that I've included a toggle button to switch between SI and American Engineering Units, which works in the input screen, but won't calculate (yet another bug I don't get)
To top it off, the units string I've defined won't display in the final display box either. I have no idea why.
If anyone could help debug this biatch, words couldn't describe how grateful I would be; I don't want to think about the time I have put into looking over each line, only to think that the program should run flawlessly.
Again thank you everyone for considering my conundrum :hi:
PS Also, I would be happy to email this file if anyone wants to take a further look at it.