Apply a Function to a Button in a Form

G

Guest

I am trying to apply a function (wchich is contained in a module) to a button
in a user input form that will read the value in once field of the form and
convert that value to a different format in a different field in the form.

Here is the function that I am trying to apply:

Sub DegToDMS(ByVal L As Double, D As Integer, M As Integer, S As Double)
'
' Converts a decimal degree to Degrees, Minutes, and Seconds
' Seconds may contain up to 3 decimal places.
' e.g. 15.5 -> 15,30,0
'
D = Int(L)
L = (L - D) * 60
M = Int(L)
S = Val(Format((L - M) * 60, "#.###"))
End Sub

Ideally, the user can input a decimal degrees value in one field, then click
the button, which will display the degrees minutes seconds value in another
field. I have a fair sense of how to create the button, but not how to apply
the function to where it will read from the defined field and populate a
different field. If anyone has any pointers they would be much appreciated.
Thanks, Ruben.
 
G

Guest

Jose,

Your function is not a function at all, it is a subroutine. A generally
returns a single value. If you write your function correctly, you can use it
as the control source for an unbound text box.

Public Function DegToDMS(ByVal L As Double) as string

'Converts a decimal degree to Degrees, Minutes, and Seconds
'Seconds may contain up to 3 decimal places.
'e.g. 15.5 -> 15,30,0

D = Int(L)
L = (L - D) * 60
M = Int(L)
S = Val(Format((L - M) * 60, "#.###"))

DegToDMS = D & "," & M & "," & S
End Sub

Select the control that you want to have this value displayed in (I'll call
it txt_DMS). Select the control Source property and enter: =
DegToDMS(me.txt_Degree)

Then in the command buttons click event, enter:

me.txt_DMS.Requery
 
G

Guest

Dale,

thanks very much for your reply.

I attempted applying what you recommended I created a module called DegtoDMS
in my database using the subroutine that you specified. I created a new text
box and for the control source property I entered "= DegToDMS(me.DegToDMS)"

Then for the button's click event I entered me.DegToDMS.Requery

When I tried to enter a decimal degrees value in the text box, a message
appear saying that "Control can't be edited; it's bound to the expression
'DegToDMS(me.DegToDMS)'. I modified what you had called "txt_Degree", calling
it instead "DegToDMS".

I'm hoping that eventually I can enter a decimal value (e.g., 120.456789) in
one box, and clicking the button will return the equivalent degrees minutes
seconds value in another text box (both text boxes correspond to values
stored in fields within a table).

I'm fairly new to applying subroutines to command buttons on a user form.
Any further suggestions are, of course, appreciated. Thanks again, J. Ruben.
 

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