Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Am new to vb and functions . How

Status
Not open for further replies.

odessa79

Programmer
Sep 16, 2003
31
US
Am new to vb and functions .

How would I create a function for date conversion?
here is my code for date conversion.
How can I pass a date field into a function and receive a value back into my txtfield?

Dim fldDate As Double
Dim fldMyDate As Date
Dim fldMyDateStr As String

fldDate = fldReceiveDate
fldMyDateStr = Trim(Str(fldDate))
fldMyDateStr = Right(Left(fldMyDateStr, 6), 2) & "/" & _
Right(fldMyDateStr, 2) & "/" & Left(fldMyDateStr, 4)
 
To put it into a function try

Function ConvertToString(fldReceivedDate as Date) as String

Dim fldDate As Double
Dim fldMyDate As Date
Dim fldMyDateStr As String

fldDate = fldReceiveDate
fldMyDateStr = Trim(Str(fldDate))
fldMyDateStr = Right(Left(fldMyDateStr, 6), 2) & "/" & _
Right(fldMyDateStr, 2) & "/" & Left(fldMyDateStr, 4)

ConvertToString = fldMyDateStr

End Function


Then in your code just say

Text1 = ConvertToString(WhateverTheDateIs)

Mark

The key to immortality is to make a big impression in this life!!
 
This is the same question as you asked in thread222-706358. Why start a new thread?

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"A computer program does what you tell it to do, not what you want it to do." -- Greer's Third Law
 

odessa79,

Perhaps a look at FAQ222-2244 might be of help because your question in this thread is not quite the same as the question in your previous Thread222-706358. You have changed the input from numeric to date between the two.

Spellman,

That will work only if the date passed in is in the format of yyyymmdd which is not a normal date format (see odessa79's previous Thread222-706358).

Also odessa79 you can find information in the help files on how to create subs and functions.

CCLINT [lol]

 
Warning :p

You are declaring a variable that you are not using...

Code:
Dim fldMyDate As Date

--
Luis MX
 
Spellman

When I do this I can see that fldReceivedDate has value but when I try to move it fldDate = fldReceiveDate its says the field is EMPTY?

Function ConvertToString(fldReceivedDate As Double) As String

Dim fldDate As Double
Dim fldMyDate As Date
Dim fldMyDateStr As String

fldDate = fldReceiveDate
fldMyDateStr = Trim(Str(fldDate))
fldMyDateStr = Right(Left(fldMyDateStr, 6), 2) & "/" & _
Right(fldMyDateStr, 2) & "/" & Left(fldMyDateStr, 4)
ConvertToString = fldMyDateStr

End Function
 
Odessa I think your doing some strange stuff with your dates. What I posted was

Function ConvertToString(fldReceivedDate as Date) as String

and you changed it to

Function ConvertToString(fldReceivedDate as Double) as String

you should have changed
Dim fldDate As Double

to

Dim fldDate As Date.

I do think that the way your converting is way off as well though. Have a look at Day, Month and year functions which may be of more use to you.





Mark

The key to immortality is to make a big impression in this life!!
 
Erm, have you considered format(dtdate,"mm/dd/yyyy")?
 
When I do this I can see that fldReceivedDate has value but when I try to move it fldDate = fldReceiveDate its says the field is EMPTY?

The answer is: You have one variable called "fldReceivedDate", and another called "fldReceiveDate", as per your code:

Code:
Function ConvertToString(fldReceivedDate As Double) As String
fldDate = fldReceiveDate

You should set the "Tools\Options\Editor\Code Settings\Require Variable Declaration" CheckBox in Visual Basic - this will highlight such things in future. You will need to add "Option Explicit" as the very first line of your module too.

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"A computer program does what you tell it to do, not what you want it to do." -- Greer's Third Law
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top