VB Dir function - not working? (1 Viewer)

Splinters

Registered User.
Local time
Today, 05:28
Joined
Sep 6, 2007
Messages
67
I have a form with 2 objects:

Text48 - A text box object which will have either a path to an image file, or a dummy path ("c:\BowlPhotos\Thumbs\tmb.jpg").

Image58 - An Image Object with an empty Picture field that is filled by the VB.

The Text48 value is determined by the value of another text box, generated using an equation that works (so I left it out to avoid confusion).

The intent is to bring up the record and, if a product exists AND an image exists, show the image for that record. Otherwise, show the default "no image" jpg.

I am using the following 2 VB functions:
=============================
Private Sub Form_Current()
On Error Resume Next

If Me.Text48.Value = "c:\BowlPhotos\Thumbs\tmb.jpg" Then
Me.Image58.Picture = "C:\BowlPhotos\Thumbs\NoBowltmb.jpg"
Else
If Dir(Me.Text48.Value) Then
Me.Image58.Picture = Me.Text48.Value
Else
Me.Image58.Picture = "C:\BowlPhotos\Thumbs\NoBowltmb.jpg"
End If
End If

End Sub
-------------------------------------------
Private Sub Form_Load()
On Error Resume Next

If Me.Text48.Value = "c:\BowlPhotos\Thumbs\tmb.jpg" Then
Me.Image58.Picture = "C:\BowlPhotos\Thumbs\NoBowltmb.jpg"
Else
If Dir(Me.Text48.Value) Then
Me.Image58.Picture = Me.Text48.Value
Else
Me.Image58.Picture = "C:\BowlPhotos\Thumbs\NoBowltmb.jpg"
End If
End If

End Sub
===============================

There are 3 possible conditions:

1: No product exists. The default text in Text48 is the "NoBowltmb.jpg" path - so the result should show the "NoBowltmb.jpg" image, which it does.

2: A product does exist, and there is an image of it. The text in Text48 is the generated path to that image, and the image does exist - so the result should show the bowl image, which it DOES do.

3: A product does exist, but there is no image of it yet. The text in Text48 is the generated path to that bowl's image, but the image does not yet exist - so the result should show the "NoBowltmb.jpg" image, which it does NOT always do. What it does instead is retain the previous image for the previous record.

Example: If the previous record was for bowl Ash08-021, and the image exists (and was shown for THAT record), the Ash08-021 image shows up in the NEXT record IF there is no image for THAT NEW record. IF, on the other hand, the previous record was also a no image case (and showed the default image), it shows the default NoBowltmb.jpg" image. Of course, if the prior record showed an image from IT'S prior record, that image shows up! Sort of a self replicating error...

Case 3 is the problem - I need it to show either the current selected bowl's image (if it exists) or the default no image (if the image does not exist).

I had hoped that the "Dir" function in the inner If segment would determine if the image existed, and take the appropriate action. It does not seem to do this.

Any ideas?
 

Fuse3k

Registered User.
Local time
Today, 08:28
Joined
Oct 24, 2007
Messages
74
Try:

Code:
If Not Dir(Me.Text48.Value, vbDirectory) = vbNullString Then
   'pathfound
Else
   'pathnotfound
End if
 

Splinters

Registered User.
Local time
Today, 05:28
Joined
Sep 6, 2007
Messages
67
Try:

Code:
If Not Dir(Me.Text48.Value, vbDirectory) = vbNullString Then
   'pathfound
Else
   'pathnotfound
End if

OK, that helped.

I tried the following:
Code:
Dim nullStr As String
Dim defaultStr As String
Dim currStr As String

nullStr = "c:\BowlPhotos\Thumbs\tmb.jpg"
defaultStr = "C:\BowlPhotos\Thumbs\NoBowltmb.jpg"
currStr = Me.Text48.Value

Me.Image58.Picture = nullStr

If Not Dir(currStr, vbDirectory) = currStr Then
   Me.Image58.Picture = currStr
Else
   Me.Image58.Picture = defaultStr
End If
[end code]

[B]That works[/B] - in that it gives the null image both when there is no product, [B]AND[/B] when there is a product but no image - but provides the proper image when one exists.

[I][B]But[/B][/I] the [B]else[/B] code does not seem to work, since it does not show the default "[I]No Image[/I]" image when a product exists and there is no existing image...

This will suffice for now - but I would like to know why the else code does not seem to work...

Thanks
 

Splinters

Registered User.
Local time
Today, 05:28
Joined
Sep 6, 2007
Messages
67
After looking at this a bit more, I realized it could be done easier.

Code:
Dim defaultStr As String
Dim currStr As String

defaultStr = "C:\BowlPhotos\Thumbs\NoBowltmb.jpg"
currStr = Me.Text48.Value

Me.Image58.Picture = defaultStr

If Not Dir(currStr, vbDirectory) = currStr Then
   Me.Image58.Picture = currStr
End If
[end code]

"C:\BowlPhotos\Thumbs\NoBowltmb.jpg" has text saying there is no image found.

Then I used that to make a blank image of the same size for "C:\BowlPhotos\Thumbs\tmb.jpg", the path when there is no product. 

This gives me the 3 possibilities I needed.

Thanks for the help.
 

doco

Power User
Local time
Today, 05:28
Joined
Feb 14, 2007
Messages
482
Having similar problem with using Me.Image111.Picture = szPath throwing an error 'cannot open, etc' Is there some special magic associated with using a string variable for the path instead of a hard coded path? I have tried putting CHR(34) before and aft but nothing works accept hard coding the actual path and that is absurd.

TIA
 

doco

Power User
Local time
Today, 05:28
Joined
Feb 14, 2007
Messages
482
I got it. Evidently the textbox is passing some leading and or trailing spaces. So

Code:
...
    Me.Image111.Picture = TRIM$(szPath)
...

does the trick.
 

Splinters

Registered User.
Local time
Today, 05:28
Joined
Sep 6, 2007
Messages
67
I got it. Evidently the textbox is passing some leading and or trailing spaces. So

Code:
...
    Me.Image111.Picture = TRIM$(szPath)
...
does the trick.

Interesting...mine seems to work without the trim. Not being an "expert" on Access - not in the slightest - I can't say why your solution is different. Using a string var should act the same in both cases...My best guess is that our text boxes may be set up slightly different. Is there a default string in your text box that might have included the spaces?
 

doco

Power User
Local time
Today, 05:28
Joined
Feb 14, 2007
Messages
482
I am reading in this case, a field from an Access table that is of hyperlink data type. If you view the rs.Fields("MyField").Value from a hyperlink type you will see it has a '#' attached the beginning and ending of the actual path string. Therefore, I had to use code to strip the leading and trailing '#'. There should not have been any leading and trailing spaces but evidently there are. So, The TRIM$(string) function was being used.

I am at home now but will post the code when I get to the office tomorrow if interested.
 

Users who are viewing this thread

Top Bottom