Validation - No Spaces (1 Viewer)

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
Hi All

I have a text box that holds the name of an image - and i want users to NOT use a space in the filename.

Is it possible to validate for No Spaces? I searched on this forum but could not spot an answer....

Thanks

ian
 
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,379
Sure it is.

General logic:
Read the textbox value
Is there a value (>"")
Could do Instr(textboxvalue," ") if >0, then there is a space

If you want to remove all spaces in a string, you could parse each character, and if it
is a space, ignore that character in the output.

Good luck.

Here is a link for "somewhat similar" vba. You can adjust to extract space character.
 
Last edited:

sneuberg

AWF VIP
Local time
Today, 01:43
Joined
Oct 17, 2014
Messages
3,506
If the user can specify an actually file name you might want to consider other characters too. Below is a function we use which just removes the ones that my research showed were illegal.

Code:
Public Function RemoveIllegalFileCharacters(strFileName As String) As String
 
strFileName = Replace(strFileName, "<", "")
strFileName = Replace(strFileName, ">", "")
strFileName = Replace(strFileName, ":", "")
strFileName = Replace(strFileName, """", "")
strFileName = Replace(strFileName, "/", "")
strFileName = Replace(strFileName, "\", "")
strFileName = Replace(strFileName, "|", "")
strFileName = Replace(strFileName, "?", "")
strFileName = Replace(strFileName, "*", "")
RemoveIllegalFileCharacters = strFileName
 
End Function
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,379
Another approach you might consider:

Is there a common directory/folder for these files?
If so, you might want to provide a listbox or combo, to display available files,'
then let the user select the file/image. This way there will be no spelling mistakes and no spaces in file names.

You could research FileDialog for more info.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:43
Joined
May 7, 2009
Messages
19,229
use the Keypress event of your textbox:

Private Sub Text0_KeyPress(KeyAscii As Integer)
'ignore if space is typed.
If KeyAscii = vbKeySpace Then KeyAscii = 0
End Sub
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
The user does click on a button which brings up all the jpg's and they choose one, and the name goes into the field. Problem has been that when the jpg is saved it is often saved with a space in the name.

But now with this code in the afterupdate property:

Code:
Dim Msg
Msg = "THERE ARE SPACES IN THE FILE NAME"
If InStr(companylogo.Value, " ") <> 0 Then
Response = MsgBox(Msg)
End If
This seems to catch any spaces in the filename.

Thanks to all for your suggestions!

Regards

Ian
 

sneuberg

AWF VIP
Local time
Today, 01:43
Joined
Oct 17, 2014
Messages
3,506
How are the spaces getting into the file name? If the users are putting them in why not just disable the field and if it's not the users then you have a bug you should track down.
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
How are the spaces getting into the file name? If the users are putting them in why not just disable the field and if it's not the users then you have a bug you should track down.

Another staff member collects images that we can use for publicity purposes, and save them in a folder called images - this is all outside access environment. They often forget not to use spaces in the names.

So, when a user in access wants to associate one of the images to a record, they can click on a get file button and up comes the list of images saved (some with spaces in the name), they click on the correct one, and this name is stored in the field.

But at least now, if they try and save one of the these filenames with spaces, access will pop up a message warning them.

thanks for your input.

ian
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
use the Keypress event of your textbox:

Private Sub Text0_KeyPress(KeyAscii As Integer)
'ignore if space is typed.
If KeyAscii = vbKeySpace Then KeyAscii = 0
End Sub

Thanks fo your suggestion - as you might have gathered by now, the user chooses the filename, rather than typing it in, but i will bear this suggestion in mind for another field where they do type in info, so thanks!
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,379
It sounds like a little training for the users is in order. This is really a procedure issue.

If you want to control the names, you could also (separately) review the image names in the folder(s), and do a rename if there are spaces in the file/image names.
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
It sounds like a little training for the users is in order. This is really a procedure issue.

If you want to control the names, you could also (separately) review the image names in the folder(s), and do a rename if there are spaces in the file/image names.

Yeah, training has happened, staff were informed but you know what it is like - there is even some text below the field in RED saying "no spaces in this field please".

They do have the ability now to rename the image name when they click on the button to show the list of images.

Thanks for your reply.

Ian
 

sneuberg

AWF VIP
Local time
Today, 01:43
Joined
Oct 17, 2014
Messages
3,506
Why do you care if there are spaces in the file names? File names don't look or function better without spaces or do they somehow?
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
Why do you care if there are spaces in the file names? File names don't look or function better without spaces or do they somehow?

Well our database drives our website www.artsalive.co.uk and also our weekly email alert, and there is often strange behaviour with images if there is a space in the filename.

Ian.
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,379
OK let's go back to the basic issue. You asked if you could do some validation to check for spaces in the value of a text box.

But, as more posts evolve, it seems the real question is:

- is there an issue with spaces in file names
Probably not, as sneuberg said.

If it is your policy to not have spaces in file names, then to avoid same:
-review files in the folder/directory, and rename as appropriate

Good luck.


Just saw your latest post. I'd preprocess the files in the folder and rename as appropriate.
The rationale is --it disrupts/hinders your web online activity which could impact returning users.
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
Hi

I may not have been clear enough in a posts below - but i have, thanks to you guys, a solution that works well for me.

A message pops up if someone tries to enter a filename with a space in it.
Job done!

And I can't thank you all enough...

Ian
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,379
I'm confused now.
If a person collects an image and saves it in a file with a space in the name,
the operating system merrily stores and records that filename in a folder.

You have a program that reads the filenames from that folder.
If there is a space in the filename, you display a message.

How exactly is the problem solved??? A few details please.

The image file still exists. The name still contains a space.
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
I'm confused now.
If a person collects an image and saves it in a file with a space in the name,
the operating system merrily stores and records that filename in a folder.

Correct so far!

You have a program that reads the filenames from that folder.
If there is a space in the filename, you display a message.

The program is the access database. Say I am on a record for the film "Crimson Peak", and i want to assign an image name for that film. I click on the button that produces a list of all images (GET JPGs button), i find the one for crimson peak, click on it and the name of the image is stored in the appropriate field.

The person that saved the image originally, saved it as "crimson peak.jpg" instead of "crimsonpeak.jpg"

How exactly is the problem solved??? A few details please.

The image file still exists. The name still contains a space.

Now, if I click on the name "crimson peak.jpg", access will pop up a message reminding me that it won't save until i have taken out the spaces.

by clicking the GET JPG's button again, I am able to rename the image to crimsonpeak.jpg, then access will let me save the record.

It may not be perfect, but it is a great step forward for me!

Hope this is clear enough...

Ian
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,379
Ok, I'm following. My question now is --why the message?

by clicking the GET JPG's button again, I am able to rename the image to crimsonpeak.jpg, then access will let me save the record.

instead of you manually renaming the file (if that's what is happening now), why not do it programmatically --without the message.
 

iankerry

Registered User.
Local time
Today, 09:43
Joined
Aug 10, 2005
Messages
190
Well, good point jdraw. The simplest answer is that i think this might be beyond my skills to write such a routine.

I would have to get access to rename the jpg image, before it puts the images name into the field, as they must both have to have the same name.

At least with the current solution each time a space appears in an image name, folk are reminded that this isn't allowed, and this may trickle through to both of the people that use the database.

Have a great weekend,

ian
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:43
Joined
Jan 23, 2006
Messages
15,379
Thanks Ian,

Have a great weekend also.

I have created at a routine based on our discussion.
This is Access. It will review jpg files in a known folder. It checks to see if there is 1 or more spaces in a file name. If there is/are space(s), then remove the spaces and rename the file. I have tested it on my machine (Lenovo laptop/win 8.1/ access 2010).

Perhaps it will be useful to your set up. (at least something to think about)
Code:
'---------------------------------------------------------------------------------------
' Procedure : RenameJPGsWithSpacesInName
' Author    : mellon
' Date      : 04/03/2016
' Purpose   : Based on a thread at
'http://www.access-programmers.co.uk/forums/showthread.php?p=1478075#post1478075
'
'This routine will review files (currently jpg) at a specific folder. If file name contains
'1 or more spaces, remove those spaces and rename the file. The routine requires a reference to the
'FileScriptingObject   *****************
'A brief summary is displayed in the immediate window.
'
'example:
'~~~~~~~
'
'Original filename na      ut4.jpg
'   Final filename naut4.jpg
'****na      ut4.jpg changed on disk to naut4.jpg    at 04/03/2016 1:06:29 PM
'
'Original filename Query Memo.jpg
'   Final filename QueryMemo.jpg
'****Query Memo.jpg changed on disk to QueryMemo.jpg    at 04/03/2016 1:06:29 PM
'
'Number of jpgs reviewed: 236
'Number of names changed: 2


'---------------------------------------------------------------------------------------
'
Sub RenameJPGsWithSpacesInName()

      Dim fso As Object, fsoFolder As Object, fsoFile As Object
      Dim strPath As String
      Dim filecount As Integer       'count of jpgs processed
      Dim NumNamesChanged As Integer 'count of filenames changed
      Dim strNameInit As String, strNameFinal As String, strNameWorking As String
      
10       On Error GoTo renameJPGswithSpacesInName_Error

20    strPath = "C:\users\mellon\documents\"  'my directory--YOU have to change to your folder !!!!!!!!!!
30    Set fso = CreateObject("Scripting.FileSystemObject")
40    Set fsoFolder = fso.GetFolder(strPath)
50    For Each fsoFile In fsoFolder.Files
60        If fsoFile.name Like "*.jpg" Then
70            filecount = filecount + 1
80            If InStr(fsoFile.name, " ") > 0 Then  'filename has a space
90                strNameInit = fsoFile.name             'original filename
100               strNameWorking = strNameInit           'set up fiename to adjust
110               While InStr(strNameWorking, " ") > 0
120                   strNameWorking = Replace(strNameWorking, " ", "")    'remove a space
130               Wend
140               strNameFinal = strNameWorking
150               Debug.Print "Original filename " & strNameInit
160               Debug.Print "   Final filename " & strNameFinal

                  'change the actual file on disk
                  NumNamesChanged = NumNamesChanged + 1
170               fsoFile.name = strNameFinal
180               Debug.Print "****" & strNameInit & " changed on disk to " & strNameFinal & "    at " & Now() & vbCrLf
190           End If
200
210       End If

220   Next fsoFile
230    Debug.Print "Number of jpgs reviewed: " & filecount & vbCrLf _
                 & "Number of names changed: " & NumNamesChanged

240      On Error GoTo 0
250      Exit Sub

renameJPGswithSpacesInName_Error:

260       MsgBox "Error " & Err.Number & " in line " & Erl & " (" & Err.Description _
                        & ") in procedure renameJPGswithSpacesInName of Module ModuleTesting_CanKill"
End Sub

Good luck with your project.
 

Users who are viewing this thread

Top Bottom