Subroutine Help

  • Thread starter Jose Ruben Gonzalez-Baird
  • Start date
J

Jose Ruben Gonzalez-Baird

Hi,

I can't make out what's wrong with my code. I'm trying to code a button that
will convert a value from one field in a form to a degrees minutes seconds
value in another field on the form. When I tie the following code into a
button, I get an error: "Compile error:Expected End Sub".

Private Sub Convert_DD_to_DMS_Click()
Function DegToDMSStr(ByVal L As Double) As String
'
' Converts a decimal value to Degrees, Minutes, and Seconds
' Processes Seconds up to 3 decimal places.
' e.g. 15.5 -> 15 30' 0"
'
Dim D As Integer, M As Integer, S As Double
D = Int(L)
L = (L - D) * 60
M = Int(L)
S = Val(Format((L - M) * 60, "#.###"))
Me.[Latitude_DMS] = D & " " & M & "' " & S & """"
End Function
End Sub
 
M

Marshall Barton

Jose said:
I can't make out what's wrong with my code. I'm trying to code a button that
will convert a value from one field in a form to a degrees minutes seconds
value in another field on the form. When I tie the following code into a
button, I get an error: "Compile error:Expected End Sub".

Private Sub Convert_DD_to_DMS_Click()
Function DegToDMSStr(ByVal L As Double) As String
'
' Converts a decimal value to Degrees, Minutes, and Seconds
' Processes Seconds up to 3 decimal places.
' e.g. 15.5 -> 15 30' 0"
'
Dim D As Integer, M As Integer, S As Double
D = Int(L)
L = (L - D) * 60
M = Int(L)
S = Val(Format((L - M) * 60, "#.###"))
Me.[Latitude_DMS] = D & " " & M & "' " & S & """"
End Function
End Sub


You can not define a function inside another procedure. The
structure of the code should be more like:

Private Sub Convert_DD_to_DMS_Click()
Me.[Latitude_DMS] = DegToDMSStr( ??? )
End Sub

Function DegToDMSStr(ByVal L As Double) As String
'
' Converts a decimal value to Degrees, Minutes, and Seconds
' Processes Seconds up to 3 decimal places.
' e.g. 15.5 -> 15 30' 0"
'
Dim D As Integer, M As Integer, S As Double
D = Int(L)
L = (L - D) * 60
M = Int(L)
S = Val(Format((L - M) * 60, "#.###"))
DegToDMSStr = D & " " & M & "' " & S & """"
End Function


The S calculation is strange. Shouldn't S be declared as a
string and the calculation be:
S = Format((L - M) * 60, "#.###")
 
J

Jose Ruben Gonzalez-Baird

What does the ??? mean in the following:

Private Sub Convert_DD_to_DMS_Click()
Me.[Latitude_DMS] = DegToDMSStr( ??? )
End Sub

Also the S calculation you referred to is almost exactly the same, except
the original uses Val(). I'm assuming that has something to do with the
following(?):

Function DegToDMSStr(ByVal L as Double)

Thanks much for your response. Ruben.
 
M

Marshall Barton

The ??? needs to be replaced by a reference to whatever
contains the value to be converted.

Check VBA Help for details of each function you are not
completely familiar with so you don't make assumptions about
what purpose they serve.

Val converts the leading numeric portion of a text string to
a number of an appropriate type. The way you were
calculating S, the Format function rounded the value to 3
places and returned that as a text value, which you then
used Val to convert back to a number (not always limited to
3 places. The number is then converted back to a string
when it is concatenated into the final result. If you did
not intend to round to 3 places, then use my first
statement. If you want the result to always be 3 places,
then just use Format (without Val).
--
Marsh
MVP [MS Access]

What does the ??? mean in the following:

Private Sub Convert_DD_to_DMS_Click()
Me.[Latitude_DMS] = DegToDMSStr( ??? )
End Sub

Also the S calculation you referred to is almost exactly the same, except
the original uses Val(). I'm assuming that has something to do with the
following(?):

Function DegToDMSStr(ByVal L as Double)

Thanks much for your response. Ruben.
You can not define a function inside another procedure. The
structure of the code should be more like:

Private Sub Convert_DD_to_DMS_Click()
Me.[Latitude_DMS] = DegToDMSStr( ??? )
End Sub

Function DegToDMSStr(ByVal L As Double) As String
'
' Converts a decimal value to Degrees, Minutes, and Seconds
' Processes Seconds up to 3 decimal places.
' e.g. 15.5 -> 15 30' 0"
'
Dim D As Integer, M As Integer, S As Double
D = Int(L)
L = (L - D) * 60
M = Int(L)
S = Val(Format((L - M) * 60, "#.###"))
DegToDMSStr = D & " " & M & "' " & S & """"
End Function


The S calculation is strange. Shouldn't S be declared as a
string and the calculation be:
S = Format((L - M) * 60, "#.###")
 
J

Jose Ruben Gonzalez-Baird

Marshall,
Thanks for the reply.
The ??? needs to be replaced by a reference to whatever contains the value to be >>converted.
What does the ??? mean in the following:
Private Sub Convert_DD_to_DMS_Click()
Me.[Latitude_DMS] = DegToDMSStr( ??? )
End Sub

So does this mean that if the value to be converted is contained in the
field 'Lat_DD', then the above should read:

Me.[Latitude_DMS] = DegToDMSStr(Me.[Lat_DD])

??

It may be unrelated, but when I tried this my VBA program locked. Also is
it possible to call a function to calculate values from different fields in
the same subroutine?

EX:
Me.[Latitude_DMS] = DegToDMSStr(Me.[Lat_DD])
Me.[Longitude_DMS] = DegToDMSStr(Me.[Long_DD])

Thanks for any assistance
 
M

Marshall Barton

Jose said:
Marshall,
The ??? needs to be replaced by a reference to
whatever contains the value to be converted.
What does the ??? mean in the following:
Private Sub Convert_DD_to_DMS_Click()
Me.[Latitude_DMS] = DegToDMSStr( ??? )
End Sub

So does this mean that if the value to be converted is contained in the
field 'Lat_DD', then the above should read:

Me.[Latitude_DMS] = DegToDMSStr(Me.[Lat_DD])
Yes


It may be unrelated, but when I tried this my VBA program locked.

This is a serious issue and needs to be tracked down
immediately. Have you set a break point in the code to see
if it helps determine what is causing the problem?

I would have to see a Copy/Paste of the code before I can
comment on what you've done with it.

Also is it possible to call a function to calculate
values from different fields in the same subroutine?

EX:
Me.[Latitude_DMS] = DegToDMSStr(Me.[Lat_DD])
Me.[Longitude_DMS] = DegToDMSStr(Me.[Long_DD])

Yes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top