Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBA last character \

0 views
Skip to first unread message

Harmannus

unread,
Feb 18, 2004, 4:47:25 AM2/18/04
to
Hallo,

I have a field path in my app.

How can ik check through VBA that the last character is a \ and if not
pressent a messagebox telling that a \ is missing at the end?

Thanx for any tips!

Regards,

Harmannus


James Goodman

unread,
Feb 18, 2004, 6:32:27 AM2/18/04
to
The best way is probably to use the instr function:

Dim pos as Integer
pos = Instr(Right(StringToSearch,1 ), "/")

If pos > 0 Then 'Last char not a /
StringToSearch = StringToSearch & "/" 'Add the slash automatically
End If

--
James Goodman
MCSE MCDBA
http://www.angelfire.com/sports/f1pictures/
"Harmannus" <hexe...@hotmail.com> wrote in message
news:ugZp3Qg9...@TK2MSFTNGP09.phx.gbl...

Harmannus

unread,
Feb 18, 2004, 8:40:01 AM2/18/04
to
Hallo,

I added your code to the after update event of the myPath field but get a
error "argument not optional.". What did i do wrong? Thanx in advance for
your help!

Private Sub MyPath_AfterUpdate()
Dim pos As Integer
pos = InStr(Right(Me.MyPath, , 1), "/")


If pos > 0 Then 'Last char not a /

Me.MyPath = Me.MyPath & "/" 'Add the slash automatically
End If
End Sub

Regards,
Harmannus


"James Goodman" <ja...@norton-associates.REMOVE.co.uk> wrote in message
news:c0vigb$llk$1...@hercules.btinternet.com...

sanfu

unread,
Feb 18, 2004, 11:36:12 AM2/18/04
to
Harmannus,

There is an extra comma in the Right() function

pos = InStr(Right(Me.MyPath, , 1), "/")

^^
should be

pos = InStr(Right(Me.MyPath, 1), "/")

BTW, since the path is entered, you might need to check for (and remove) a space as the last char, also.

pos = InStr(Right(Trim(Me.MyPath), 1), "/")

The Trim() function removes leading and trailing spaces.

Steve
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)

----- Harmannus wrote: -----

Harmannus

unread,
Feb 18, 2004, 1:38:17 PM2/18/04
to
Hallo,

Thanx for the reply.

Still can't get the code to work.

Private Sub Path_BeforeUpdate(Cancel As Integer)
Dim pos As Integer
pos = InStr(Right(Trim(Me.Path), 1), "\")
If pos > 0 Then 'Last char not a \
Me.Path = Me.Path & "\" 'Add the slash automatically
End If
End Sub

A path without a \ doesnt'get a \ added. A path with a \ gives a error.

Any suggestion on how to correct this?

Regards,

Harmannus

"sanfu" <sanfu_SPAM@BLOCKER_techie.com> wrote in message
news:E92E310F-0232-4574...@microsoft.com...

SteveS

unread,
Feb 18, 2004, 10:31:12 PM2/18/04
to
Not knowing what the rest of your form looks like, I was able to use
either the FORM before update or the control after update. I would use
the control afterupdate event.


---------
Private Sub path_AfterUpdate()
Dim pos As Integer, str As String


pos = InStr(Right(Trim(Me.Path), 1), "\")

If pos < 1 Then 'Last char not a \


Me.Path = Me.Path & "\" 'Add the slash automatically
End If
End Sub

-------

NOTE: I had to change the "If pos" to If pos < 1 ; which means the "\"
was not found.


this also works
------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim pos As Integer, str As String


pos = InStr(Right(Trim(Me.Path), 1), "\")

If pos < 1 Then 'Last char not a \


Me.Path = Me.Path & "\" 'Add the slash automatically
End If

End Sub
-------


--

0 new messages