VBA last character \

  • Thread starter Thread starter Harmannus
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
Back
Top