Visual Basic help

P

Thread Starter

Paul

Can anybody check this code for me
Private Sub CmdView_Click()
Dim extension As String
Dim returnValue

extension = UCase$(Right$(txtFile.Text, 3))
If Dir$(txtFile.Text) = " " Then
MsgBox "File is not found!", vbExclamation


ElseIf extension = "TXT" Then
returnValue = Shell("Notepad" & txtFile.Text, 1)

ElseIf extension = "BMP" Then
returnValue = Shell("Pbrush" & txtFile.Text, 1)

End If
End Sub

It didn't work !!!!
 
G

Guy Rousseau

Corrections inserted below in your text.

> Can anybody check this code for me
> Private Sub CmdView_Click()
> Dim extension As String
> Dim returnValue
>
> extension = UCase$(Right$(txtFile.Text, 3))
> If Dir$(txtFile.Text) = " " Then

If Dir$(txtFile.Text) = "" Then 'remove the space between the quotes

> MsgBox "File is not found!", vbExclamation
>
>
> ElseIf extension = "TXT" Then
> returnValue = Shell("Notepad" & txtFile.Text, 1)

returnValue = Shell("Notepad " & txtFile.Text, 1) ' Added a space after Notepad to make a distinction between the application called and the parameters.

>
> ElseIf extension = "BMP" Then
> returnValue = Shell("Pbrush" & txtFile.Text, 1)
>
> End If
> End Sub
>
> It didn't work !!!!

I tested it and it works. (try to indent your code and use End If even if not mandatory;-)

Guy
 
A

Andrew Archibald

Noticed a couple things
First
> If Dir$(txtFile.Text) = " " Then
should be equated to a null string "" not a space
If Dir$(txtFile.Text) = "" Then

Second the shell commands
> returnValue = Shell("Notepad" & txtFile.Text, 1)
should have a space added after the application name
returnValue = Shell("Notepad " & txtFile.Text, 1)

Hope this helps,
Andrew Archibald
Object Controls, LLC
 
First, try putting a space after 'Pbrush' and 'Notepad'.

Second, make sure you have the entire path, filename and extension of the file in txtFile textbox. The Dir function will probably never return a space. You should be checking for an empty string ("").

-Matt
 
The Dir command returns a "empty string" if the
file dos'nt exist, but you check for a space!

" " shuld bee ""

> If Dir$(txtFile.Text) = " " Then <--(Wrong)

> If Dir$(txtFile.Text) = "" Then <--(Right)
 
Could you be a bit more specific?

I was able to execute your code fragment and it did call up notepad with the specified file. However, in your shell script you do need a space between 'notepad' and the filename. Another problem is that the command 'Dir$' returns an empty string when no file is found and your test is looking for 'Dir$' equal to one space, Chr$(32).

[email protected]
 
You need to add a space between the program you are executing with the Shell function and the filename you are passing to that program as a parameter. Your line of code
returnValue = Shell("Notepad" & txtFile.Text, 1)
should actually read
returnValue = Shell("Notepad " & txtFile.Text, 1)
This also applies to the line calling the Paint program.

Regards,

John Messinger
 
G

Greg Schiller

First of all what didntt work. Step throug your program. look at your vars in the debug window.

or use

Try fname$ = Dir$(txtfile.text)
msgbox fname$

Next Try

if Dir$(txtfile.text) = "" then
...

instead.
Or check the length of your return value

if Len(Dir$(txtfile.text)) = 0 then
...


Good luck.

Greg Schiller
www.autoamtica.biz
 
H

Hevelton Araujo Junior

Two things:

1. The line
If Dir$(txtFile.Text) = " " Then
the quotes should not have space between them

2. The program names in the Shell statement ("Notepad" and "Pbrush") should have a space after just as if written in the cmd line.

Hope it helps !

--
Hevelton Araujo Junior
Gerência de Engenharia
Tel.: +55-31-34275852
Fax.: +55-31-34912077
[email protected]
 
A

Arturo Martinez

Another thing that I hope help you:

returnValue = Shell("Pbrush" & txtFile.Text, 1)

Shell in VB, for Win98 and upper, need variable 'returnValue' has dimensioned for LONG. In Declare section of Basic Module you say default dimen
 
You must leave spave after Notepad i.e.:

runnp = Shell("notepad.exe " & txtfile.text , vbNormalFocus)

the space is neccessary
 
> Hi!!
> How I can close the File?? <

go to the main visual basic program and end the sub program.
 
Top