Find and Replace Programatically

SteveC24

Registered User.
Local time
Today, 23:00
Joined
Feb 1, 2003
Messages
444
Hello,

I have a list of dates of births in a table, but the way they are always imported (from some piece of crappy software somewhere) is in the format of: 01.01.2005 (using full stops).

Access doesn't like this, and insists on slashes, or dashes. Now, if I do a find/replace, it works perfectly, just replacing all the full stops with slashes. However, I want to do this quite regularly...any ideas how to do it programatically in VBA?

Thanks!

Any help much appreciated!
 
You should be able to modify the enclosed code to Suit.


Code:
'===============================================================================
'Purpose to modify the date
'from Excel so that they would work in Access. The date in the excel table was in
'text format, which Access didn't like!!!!

'please note the access prefers American format dates, so double check to
'make sure that your dates Have been correctly interpreted before destroying your old data!
'
'if you're not careful access interprets the
'forward slash / as a division instruction if you put the dates in a number field.

Public Function fFixDate(strDate As String) As String
Dim intLenDate As Integer
Dim x As Integer
Dim strNewDate As String
Dim strChar As String

intLenDate = Len(strDate)

For x = 1 To intLenDate
strChar = Mid(strDate, x, 1)
 If strChar = "." Then
 Let strNewDate = strNewDate & "/"
 Else
 strNewDate = strNewDate & strChar
 End If
Next x

Select Case strNewDate 'If the date returned is invalid this case statement replaced it with a default date

Case ""
strNewDate = "1/6/58"
Case "."
strNewDate = "1/6/58"
Case "/"
strNewDate = "1/6/58"

End Select

fFixDate = CDate(strNewDate) 'this don't look right!

End Function

'I have actually use this code to import dates from XL but it is not thoroughly tested! Be warned.
 
The later versions of Access have the builtin Replace() function. Somebody has posted a custom Replace() function that will work in earlier versions of Access. Try searching the forum for the Replace function.
 
I was determined to find it for myself. Ricky Hicks gets the credit for this oldie but goodie for those with an older version of Access can use this.

Code:
Public Function fReplace(ByVal Expression As String, ByVal Find As String, ByVal Replace As String) As String
Dim strTmp As String, n As Integer
 
strTmp = Expression
n = InStr(strTmp, Find)
 
Do While n > 0
  strTmp = left(strTmp, n - 1) & Replace & _
   Mid(strTmp, n + Len(Find))
  n = InStr(n + Len(Replace), strTmp, Find, 1)
Loop
 
fReplace = strTmp
  
End Function
Here is how you would use it with a query...

Code:
fReplace([FieldName],".","/")
 
Last edited:
Thanks for your input people! Much appreciated. I did take a poke around the search of the forum, and on Google, and did find the replace() function, but could only make it work on a predefined string, not on a field or anything useful!

So - I will try GHudson's suggestion first - as I think that looks less scary!

Thanks - I will let you know how it goes! :D
 

Users who are viewing this thread

Back
Top Bottom