VBA Code Assistance Requested (1 Viewer)

warlord

Registered User.
Local time
Yesterday, 23:38
Joined
Jul 26, 2014
Messages
12
Last post was too complicated. Simply...I'm using this VBA code to compute an oval measurement on a thumb. The first time the routine is called...it erases the thumb hole (Hole1) which draws it's data from a combo box where column #1 is a fraction value (7/8) and column #2 is the decimal (.875) and causes a null error. The 2nd time a value is entered the value remains and it finishes the routine. What I am trying to figure out is why the value is deleted only upon the initial entry? I also have a button which calls this routine. Everytime the button is used it causes a null error. Am I missing a statement to put the data into the Hole1 & Hole1F fields and if so...why on 2nd pass does it retain the values? Any idea's?

Private Sub OvalCalc()
Me.Hole1.Value = Me.Combo151.Column(2)
Me.Hole1F.Value = Me.Combo151.Column(1)
Dim db As Database
Dim rs As Recordset
Dim cutcount As Integer
Dim Cut As Integer
Dim CutnameH As String
Dim CutnameV As String
Set db = CurrentDb
Set rs = db.OpenRecordset("select * FROM MeasureT Where MeasureID=" & Me.MeasureID)
If OvalDegree <> 0 Then
'Calculate Oval Sin/Cos
Refresh
rs.Edit
rs.Fields("OvalVert") = Hole1
rs.Update
rs.Edit
rs.Fields("OvalDiff") = Round(OvalHoriz - OvalVert, 3)
rs.Update
rs.Edit
rs.Fields("OvalSin") = Round([OvalDiff] * Sin([OvalDegree] * 1.74532925199433E-02), 3)
rs.Fields("OvalCos") = Round([OvalDiff] * Cos([OvalDegree] * 1.74532925199433E-02), 3)
rs.Update
If OvalSin > OvalCos Then
rs.Edit
rs.Fields("OvalCuts") = Round(OvalSin / 0.036, 3)
Else
rs.Edit
rs.Fields("OvalCuts") = Round(OvalCos / 0.036, 3)
End If
rs.Update
Refresh

'Calculate # of Cuts

cutcount = Int(OvalCuts) + 1
Cut = 1

'Calculate Oval Starting point

If ThumbSlugType = "VIT: Vise IT" Then
rs.Edit
rs.Fields("OvalHX") = 0
rs.Fields("OvalVx") = 0
Else
rs.Edit
rs.Fields("OvalVx") = Hole1FR
rs.Fields("OvalHx") = Hole1LR
End If
rs.Update
Refresh

'Calculate Oval cuts

Do While Cut < 10
CutnameH = "OvalH" + Right(Str(Cut), 1)
CutnameV = "OvalV" + Right(Str(Cut), 1)
rs.Edit
If Cut > cutcount Then
rs.Fields(CutnameV) = 0
rs.Fields(CutnameH) = 0
Else
rs.Fields(CutnameV) = OvalVx - Round((OvalSin / cutcount) * Cut, 3)
If rs.Fields("LeftHand") = -1 Then
rs.Fields(CutnameH) = OvalHx - Round((OvalCos / cutcount) * Cut, 3)
Else
rs.Fields(CutnameH) = OvalHx + Round((OvalCos / cutcount) * Cut, 3)
End If
End If
rs.Update
Cut = Cut + 1
Loop
Refresh
rs.Close
End If

Set rs = Nothing
db.Close

End Sub

I have linked a copy of the database file ([FONT=&quot]https://drive.google.com/file/d/0Bxe4EISu1gzEd1JEQnZlN2REQjA/view?usp=sharing ) It is too large to upload to the website directly.


[/FONT]
 
Last edited:

JHB

Have been here a while
Local time
Today, 08:38
Joined
Jun 17, 2012
Messages
7,732
One of the problem(s) is here:
Code:
Me.Hole1F.Value = Me.Combo151.Column(1)
Hole1F is a field in the table which is bound to Me.Combo151.Column(0).
 

Cronk

Registered User.
Local time
Today, 16:38
Joined
Jul 4, 2013
Messages
2,772
"The first time the routine is called...it erases the thumb hole (Hole1)"

It does not erase the data, it sets the value of Me.Hole1 to whatever is in me.combo151.column(2) which incidentally is the third column, given that the first column is column(0)

Which lines gives you a null error? Is it that your combo only has 2 columns, column(0) and column(1)

Incidentally, you don't need to update the recordset after changing each field, only when you are finished. ie the lines in red below are superfluous.

rs.Edit
rs.Fields("OvalVert") = Hole1
rs.Update
rs.Edit

rs.Fields("OvalDiff") = Round(OvalHoriz - OvalVert, 3)
rs.Update
rs.Edit

rs.Fields("OvalSin") = Round([OvalDiff] * Sin([OvalDegree] * 1.74532925199433E-02), 3)
rs.Fields("OvalCos") = Round([OvalDiff] * Cos([OvalDegree] * 1.74532925199433E-02), 3)
rs.Update
 

warlord

Registered User.
Local time
Yesterday, 23:38
Joined
Jul 26, 2014
Messages
12
Which lines gives you a null error? Is it that your combo only has 2 columns, column(0) and column(1)

If OvalDegree <> 0 Then
'Calculate Oval Sin/Cos
Refresh
rs.Edit <-----------------------------------------(This is where it nulls out)
rs.Fields("OvalVert") = Hole1
rs.Update
rs.Edit

It does not erase the data, it sets the value of Me.Hole1 to whatever is in me.combo151.column(2) which incidentally is the third column, given that the first column is column(0)

The table which contains the combo data has 3 columns. First is an index, Hole1 is the Hole in fraction form and Hole1f is the decimal equivalent.

Still unsure why only the first run through causes a null error? The same routine is used when a hole size is selected in the same manor on the 2nd pass?

Incidentally, you don't need to update the recordset after changing each field, only when you are finished. ie the lines in red below are superfluous.

rs.Edit
rs.Fields("OvalVert") = Hole1

rs.Update
rs.Edit

rs.Fields("OvalDiff") = Round(OvalHoriz - OvalVert, 3)
rs.Update
rs.Edit

rs.Fields("OvalSin") = Round([OvalDiff] * Sin([OvalDegree] * 1.74532925199433E-02), 3)
rs.Fields("OvalCos") = Round([OvalDiff] * Cos([OvalDegree] * 1.74532925199433E-02), 3)
rs.Update

I wasn't sure if I needed to update the database once I assigned a value which was to be used in a 2nd calculation. If I don't update...will it use the newly assigned value?

Thanks in advance for your time... :)
 

Users who are viewing this thread

Top Bottom