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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace and Mid functions conflict 2

Status
Not open for further replies.

DeltaTech

Programmer
Apr 16, 2003
92
MY
Hi
Was wondering if there is some sort of conflict between the Replace and Mid functions.
Used by itself, the Mid function works fine, but when used after a Replace function call, I get the following error:

Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument: 'Mid'


Any thoughts?

[bigcheeks]
 
can you show us some code...

null strings prodecure error when you use Replace() or Mid() functions...

show us what you are trying to do...


-DNG
 
i meant to say

null strings prodecure produce error when you use Replace() or Mid() functions...

-DNG

 
Hi DNG

This is my code:

Code:
'Retrieve data from form 
1. dTempDate = Request.Form("MessageDate")
2. sMessageFrom = Request.Form("MessageFrom")

'Replace any occurences of ' and " with ¢ and £ 
3. sMessageFrom = Replace(sMessageFrom, Chr(34), Chr(162))
4. sMessageFrom = Replace(sMessageFrom, Chr(39), Chr(163)) 

'Extract the day, month and year from MessageDate 
5. dDay = Mid(dTempDate, 1, Instr(1,dTempDate,"/")-1)
6. dTempDate = Mid(dTempDate, Instr(1,dTempDate,"/")+1)
7. dMonth = Mid(dTempDate, 1, Instr(1,dTempDate,"/")-1) 
8. dTempDate = Mid(dTempDate, Instr(1,dTempDate,"/")+1)
9. dYear = dTempDate

An error occurs on the 5th line.

Essentially, I am trying to extract the date from the form which is entered in the dd/mm/yyyy format and save it in mm/dd/yyyy format.

The server is a hosted and I cannot change the regional settings so I have to programmatically manipulate the date both while saving and displaying.

If I don't do that, then I have problems with dates entered like 03/08/2005 (dd/mm/yyyy) which when saved is later intepreted as 8 Mar 2005 instead of 3 Aug 2005.

I can't think of any other way to overcome this date problem.

Any help would be appreciated.
 
if you are sure that your input dates are always in the following format...dd/mm/yyyy

then you can just do this...

Code:
<%
dTempDate="03/08/2005"

dDay=Mid(dTempDate,1,2)


dMonth=Mid(dTempDate,4,2)
dYear=Mid(dTempDate,7,4)

response.write dMonth&"/"&dDay&"/"&dYear
%>

here...the dates entered by the user all always like this 03/08/2005, the above obviously fails if the input looks like this...3/8/2005

i was just making it simple for you if the input is always the same...if not we can come up with something like more dynamic using string functions...

-DNG
 
here is the code for dynamically changing depending on the user input...

Code:
<%
dTempDate="03/08/2005"
'dTempDate="3/8/2005"

firstpos=Instr(dTempDate,"/")

secondpos=Instrrev(dTempDate,"/")

dDay=Left(dTempDate,firstpos-1)

dMonth=Mid(dTempDate,firstpos+1,secondpos-firstpos-1)

dyear=Right(dTempDate,4)

response.write dMonth&"/"&dDay&"/"&dYear
%>

try with both dTempDate values...post back if you have any questions...

-DNG
 
Hi DNG
Thanks for the quick feedback.
I anticipate that the first option will still generate an error because of the Mid function and the uncertainty of the user entered date format dd/mm/yyyy, d/m/yy, etc.
I am going to try out the second option of extracting the dates and will certainly give my feedback on it.
[bigcheeks]
 
if you also have a chance that user can enter yy instead of yyyy then you need this:
Code:
<%
dTempDate="3/8/05"

firstpos=Instr(dTempDate,"/")

secondpos=Instrrev(dTempDate,"/")

dDay=Left(dTempDate,firstpos-1)

dMonth=Mid(dTempDate,firstpos+1,secondpos-firstpos-1)


[red]dYear=Right(dTempDate,len(dTempDate)-secondpos)[/red]

response.write dMonth&"/"&dDay&"/"&dYear
%>


-DNG
 
Couple of other alternatives:

Code:
ARRAY METHOD:
dim sDateString: sDateString = "20/12/2005"  'dd/mm/yyyy'
dim aDateParts: aDateParts = split(sDateString, "/")  'can change to "-" etc
dim sNewDate

if ubound(aDateParts) = 2 then
   sNewDate = aDateParts(1) & "/" & aDateParts(0) & "/" & sDateParts(2)
else
   ... tell the user there's a problem etc...
end if

Code:
DATE FUNCTIONS:
   dim sDateString: sDateString = "25/11/2005" 'dd/mm/yyyy'
   dim dDate
   if isdate(sDateString) then
      dDate = cdate(sDateString)
      sNewDate = Month(dDate) & "/" & Day(dDate) & "/" & Year(dDate)
   else
      .... tell the user etc..
   end if

You can then use these in a function which you use when sending/receiving date info from the database.

you can also use the session.LCID, but this get's fiddly and can be easily confusing for supporting etc.

But it all works - a matter of preference really.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
yes...i was sure there are many other ways of achieveing the same thing...but i always try to keep the things simple...

-DNG
 
Hi damber
Thank you for the helpful post.
I must apologise to both you and DNG though.
In actual fact, there is no conflict. The problem was in that the Date variable was empty, and I had not coded to catch this problem. So when it came to the Mid function, an error was generated.
Sometimes we look in the wrong places, although I do wish ASP generated more friendly error messages.
I did learn something new from the both of you though, and sincerely appreciate your willingness to help.
Thanks again.
[bigcheeks]
 
check out my first post in this thread...thats what i suspected...anyways glad to be of help...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top