VBA last character \

H

Harmannus

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
 
J

James Goodman

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
 
H

Harmannus

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
 
H

Harmannus

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
 
S

SteveS

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top