Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2005 Express to VB6 1

Status
Not open for further replies.

Jhtexas

Technical User
Jan 4, 2005
59
US
I wrote a small program in 2005 Express and found out that it will not run on the Win95 OS. I was wondering if someone could show an example of what the code would look like if written in VB6?

Thanks in advance,
Jim

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("C:\Program Files\IDM Computer Solutions\UltraEdit-32\uedit32.exe")
Application.DoEvents() ' Process outstanding messages before sleeping
System.Threading.Thread.Sleep(6000)
Application.Exit()
End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
My.Computer.FileSystem.CopyFile("C:\TestFolder\test.f01", _
"C:\TestFolder\test.dat", Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)

End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
My.Computer.FileSystem.CopyFile("C:\TestFolder\test.f00", _
"C:\TestFolder\test.dat", Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)
End Sub
End Class
 
This is .Net code. For VB6, some general things:

1. There isn't a class statement that defines a form. Form definition is basically internal to VB.
2. To start an exe, use the Shell statement.
3. Radio buttons are Option buttons. CheckedChanged is Change, or probably click is better. Investigate also control arrays, as option buttons are best set up as control arrays.
4. The FileSystemObject in the Microsoft Scripting Runtime (scrrun.dll) is the est way to copy files, although it doesn't quite have all the features of the .Net FileSystem object.

I hope this gives you a walking start. :)

Bob
 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
'Start Target Application
Shell ("C:\cmm_???\cmmsorin\cmm_app.exe")
'Keep VB Application open for 3 seconds after Target Application executes
Sleep 3000
'Exit VB Application
Unload Me
End Sub
Thanks for your help. This is what I came up with. How does it look? Please understand I'm new to writing code.

Private Sub Option1_Click()
Set fso = CreateObject("Scripting.FileSystemObject")
'Copy the printing-ON file over Test.dat
fso.CopyFile "C:\cmm_???\cmmsorin\prnflag.f01", "C:\cmm_???\cmmsorin\prnflag.dat", 1
End Sub
Private Sub Option2_Click()
Set fso = CreateObject("Scripting.FileSystemObject")
'Copy the printing-OFF file over Test.dat
fso.CopyFile "C:\cmm_???\cmmsorin\prnflag.f00", "C:\cmm_???\cmmsorin\prnflag.dat", 1
End Sub
 
Well, that was quick. Nice work! Just so you know, you can put code in blocks, like this:
Code:
Here's some code
by enclosing the code in code tags. To see how, hit Preview Post and look down at the Editing Tips at the bottom of the page.

Now, one more thing: if you're using Sleep to make sure that the VB application stays open until the target app is done, there's a better way to do it. Here's an API solution from Hypetia that I use regularly:

Code:
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = &H400

Private Function IsProcessRunning(pid As Long) As Boolean
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
CloseHandle hProcess
IsProcessRunning = hProcess
End Function
So, instead of sleeping for 3000, you can sleep for, say, 100 and loop while IsProcessRunning.

You can use the above function to kill a process as well:
Code:
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Const PROCESS_TERMINATE = 1
Private Function KillProcess(pid As Long) As Boolean
Dim hProcess As Long
Do
    hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid)
    KillProcess = TerminateProcess(hProcess, 0)
    CloseHandle hProcess
Loop While IsProcessRunning(pid)
End Function
IsProcessRunning will tell you if a process is running. KillProcess will attempt to terminate a process, check to see if it's still running, and then attempt to terminate it again until successful. (Of course, if it's never successful.......)

HTH

Bob
 
One more thing: Control arrays allow you to have multiple option buttons share the same click event, so you don't have to have duplicate code. Just put two option buttons with the same name on your form. You'll be asked if you want to create a control array, say yes. One will have an index property of 0, the other will be 1. This value gets passed to the event handler. So, thus:
Code:
Private Sub optMybuttons_Click(Index as Integer) 'Note the Index argument here
Dim strExtension as string
if index = 0 then
    strExtension = "f01"
else
    strExtension = "f02"
End If
Set fso = CreateObject("Scripting.FileSystemObject")
'Copy the right printing file over Test.dat
fso.CopyFile "C:\cmm_???\cmmsorin\prnflag?." & strExtension , "C:\cmm_???\cmmsorin\prnflag.dat", 1
End Sub

HTH

Bob
 
Bob,
Thank you very much for the help. I'll try out your suggestions.

Thanks,
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top