nubie question using Left() and Right()

brad78

Registered User.
Local time
Today, 00:01
Joined
Oct 10, 2008
Messages
32
Hey all,
should be a pretty simple question but I am having problems breaking a variable which holds a short date (dd/mm/yyyy) into seperate parts. I get a mismatch error. Anyone know what I'm doing wrong here?

Code:
    Dim last As String
    Dim today As Date
    Dim myday As Integer
    Dim mymonth As Integer
    Dim myyear As Integer
 
    review = Me.content_review_period [COLOR=seagreen]'short date textbox on same form[/COLOR]
    last = Me.page_last_updated [COLOR=seagreen]'short date textbox on same form[/COLOR]
    today = Date
 
    myday = Left(last, 2)
    mymonth = Left(last, 5)
    MsgBox (mymonth) [COLOR=seagreen]'displays the error.[/COLOR]
    myyear = Right(last, 4)

Thanks
 
How about this instead:

MyDay = Day(Date)
MyMonth = Month(Date)
 
I would use the mid function. In this case, it would be:

mymonth = mid(last,3,2)

Or, you could use Bob's method.
 
I have tried this method and it still gives me a mismatch error.:(

my variable "last" holds a short day format such as last = 10/10/2008.

when I use myday = left(last, 2) I get 10
when I use myyear = right(last, 4) I get 2008
but when i use mymonth = mid(last, 4, 2) I get the mismatch error.

Hope this helps...


I would use the mid function. In this case, it would be:

mymonth = mid(last,3,2)

Or, you could use Bob's method.
 
I have tried this method and it still gives me a mismatch error.:(

my variable "last" holds a short day format such as last = 10/10/2008.

when I use myday = left(last, 2) I get 10
when I use myyear = right(last, 4) I get 2008
but when i use mymonth = mid(last, 4, 2) I get the mismatch error.

Hope this helps...

And what happens when you use

myday = Day(last)
myyear = Year(last)
mymonth = Month(last)
 
Thanks for the help Bob. I figured out the problem. When using Left() and right() the variable needs to be a string. What was happening is that I was trying to do come calculations based on the mymonth and myyear variables and with them being strings, it errored. I used the cInt() on the mymonth and myday variables and everything is working.
Thanks again.
 
however, I still don't know why you would go through all of that hassle with Left, Right, and mid and CInt etc. when what I showed will give you what you need without all of the code and conversions.
 
Try this:

mymonth = Mid(last,InStr(1,last,"/")+1,InStr(4,last,"/")-InStr(1,last,"/")-1)
 
Try this:

mymonth = Mid(last,InStr(1,last,"/")+1,InStr(4,last,"/")-InStr(1,last,"/")-1)

WWWWHHHHHHHHHYYYYYYYYYYYY?????????????????

That is the most convoluted and unecessary code ever. Just freakin' use

myMonth = Month(last)

and be done with it for heaven's sake!!!!!!!!!!
 

Users who are viewing this thread

Back
Top Bottom