Data Format (1 Viewer)

kitty77

Registered User.
Local time
Today, 14:53
Joined
May 27, 2019
Messages
713
[Mlink] = "#\comps\pdf\" & [Msample] & ".pdf#"

Msample data looks like this: 12345.SPA, so the above results is: \comps\pdf\12345.SPA.pdf

How can I remove the .SPA? So it looks like \comps\pdf\12345.pdf in vba.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:53
Joined
Feb 19, 2002
Messages
43,439
Do you want to remove ".SPA" specifically or only the period and characters to the right?
 

kitty77

Registered User.
Local time
Today, 14:53
Joined
May 27, 2019
Messages
713
need to remove the period and characters to the right.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:53
Joined
Feb 28, 2001
Messages
27,300
Do you know that there WILL be a period in the [MSample] data? Or is it a sometimes there, sometimes not case?

Code:
[Mink] = "#\comps\pdf\" & IIF( InStr( 1, [MSample], "." ) = 0, [MSample], Left( [MSample], Instr( 1, [MSample], "." ) - 1 ) ) & ".pdf#"

This would remove everything to the right of the first dot (.) it finds, but if there is no dot, it returns all of [MSample].
 

MarkK

bit cruncher
Local time
Today, 11:53
Joined
Mar 17, 2004
Messages
8,186
You can also split the text at the period, and only take the first element from the resulting array. This simplifies doc_mans code as follows...
Code:
[Mink] = "#\comps\pdf\" & Split(MSample, ".")(0) & ".pdf#"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:53
Joined
Feb 28, 2001
Messages
27,300
Thanks for the alternate viewpoint, MarkK - I don't always think about SPLIT but it surely could do the job.
 

cheekybuddha

AWF VIP
Local time
Today, 19:53
Joined
Jul 21, 2014
Messages
2,318
Will the '12345' portion always be numeric?

If so, you can just use Val()
Code:
[Mlink] = "#\comps\pdf\" & Val([Msample]) & ".pdf#"
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:53
Joined
Jan 20, 2009
Messages
12,854
Although they would work in this case, since it is also possible for folder and filenames to contain dots other than the extension separator, none of the suggestions so far should be considered as general solutions to removing the extension from a file name.

The safest way to do this is a variation in Doc's code using InStrRev() to locate the last dot.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:53
Joined
Jan 20, 2009
Messages
12,854
This simplifies doc_mans code
Developers should not be excessively focused on simplification of the appearance of the code. The simplest code is not always the most efficient code. Although I have not tested, I would expect Split() to have significant overheads., though it probably doesn't really matter much.

The desire for simple looking code often drives developers to overuse user-defined-functions when the job can be done with native SQL engine functions that, although they look more complex in the SQL, will perform better than a UDF.
 

MarkK

bit cruncher
Local time
Today, 11:53
Joined
Mar 17, 2004
Messages
8,186
You can also do...
Code:
With CreateObject("Scripting.FileSystemObject")
   [Mink] = "#\comps\pdf\" & .GetBaseName(MSample) & ".pdf#"
End With
;)
 

Users who are viewing this thread

Top Bottom