Aug 25th, 2006, 11:06 AM
#1
Thread Starter
Addicted Member
couple of simple questions
Got two questions.
1. How can I prevent the user from starting two instances of the same program?
2. I am executing a SHELL command from within my program. Is there a way to have my program wait until the SHELL command has completed?
Aug 25th, 2006, 11:08 AM
#2
Re: couple of simple questions
1. In Form_Load do
VB Code:
If App.PrevInst Then Unload Me
Aug 25th, 2006, 11:27 AM
#3
Thread Starter
Addicted Member
Re: couple of simple questions
Added
VB Code:
If App.PrevInstance then
Unload me
End
End If
I get an error when I try and execute a second copy.
RunTime Error 91
Object Variable or with Variable not set
Aug 25th, 2006, 11:31 AM
#4
Re: couple of simple questions
Please post the Form_Load sub.
Aug 25th, 2006, 11:36 AM
#5
Re: couple of simple questions
i'd recommend using Sub Main instead:
VB Code:
' In a module
Private Sub Main()
If Not App.PrevInstance Then frmMain.Show
End Sub
Aug 25th, 2006, 11:37 AM
#6
Thread Starter
Addicted Member
Re: couple of simple questions
VB Code:
Private Sub Form_Load
If App.PrevInstance then
Unload Me
End
End If
Rest of Form_Load code runs OK. It is only when I add the code at the top of Form_Load that I get the error.
Aug 25th, 2006, 11:38 AM
#7
Re: couple of simple questions
Originally Posted by
bushmobile
i'd recommend using Sub Main instead:
VB Code:
' In a module
Private Sub Main()
If Not App.PrevInstance Then frmMain.Show
End Sub
As long as the App.PrevInstance is the first thing in Form_Load the effect is the same.
Aug 25th, 2006, 11:40 AM
#8
Re: couple of simple questions
Originally Posted by
JohnDonaldson
VB Code:
Private Sub Form_Load
If App.PrevInstance then
Unload Me
End
End If
Rest of Form_Load code runs OK. It is only when I add the code at the top of Form_Load that I get the error.
Is this being done in VB6?
Aug 25th, 2006, 11:43 AM
#9
Re: couple of simple questions
Originally Posted by
MartinLiss
As long as the App.PrevInstance is the first thing in Form_Load the effect is the same.
I know, but controling the manner in which your app loads is easier in Sub Main and you don't have to think about unloading stuff etc.
Aug 25th, 2006, 11:43 AM
#10
Re: couple of simple questions
2. Try this ShellWait function (need one CommandButton):
VB Code:
Option Explicit
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const INFINITE = &HFFFF
Private Const SYNCHRONIZE = &H100000
Private Const WAIT_TIMEOUT = &H102
Public Sub ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbMinimizedFocus, Optional bDoEvents As Boolean = False)
Dim dwProcessID As Long
Dim hProcess As Long
dwProcessID = Shell(PathName, WindowStyle)
If dwProcessID = 0 Then
Exit Sub
End If
hProcess = OpenProcess(SYNCHRONIZE, False, dwProcessID)
If hProcess = 0 Then
Exit Sub
End If
If bDoEvents Then
Do While WaitForSingleObject(hProcess, 100) = WAIT_TIMEOUT
DoEvents
Loop
Else
WaitForSingleObject hProcess, INFINITE
End If
CloseHandle hProcess
End Sub
Private Sub Command1_Click()
ShellWait "notepad.exe", vbNormalFocus
MsgBox "Finished !"
'continue code...
End Sub
Aug 25th, 2006, 11:44 AM
#11
Re: couple of simple questions
Once again I ask please post your complete code for the Sub. I'm also curious as to why "then" isn't capitalized.
Aug 25th, 2006, 11:46 AM
#12
Re: couple of simple questions
Originally Posted by
bushmobile
...and you don't have to think about unloading stuff etc.
But you don't the other way either.
Aug 25th, 2006, 11:49 AM
#13
Re: couple of simple questions
you need to set the start up object to Sub Main.
Project Menu | Project Properties
change the Startup Object combobox to Sub Main
Aug 25th, 2006, 11:49 AM
#14
Re: couple of simple questions
Now I'm confused. The code I posted should be the first line in the Form_Load Sub of your main (first) form. Is it?
Aug 25th, 2006, 11:51 AM
#15
Thread Starter
Addicted Member
Re: couple of simple questions
I already have a module1.bas , so I added the code to it. Now I get multi-instances.
Aug 25th, 2006, 11:53 AM
#16
Re: couple of simple questions
The code I posted should be the first line in the Form_Load Sub of your main FORM .
Aug 25th, 2006, 11:55 AM
#17
Thread Starter
Addicted Member
Re: couple of simple questions
I first added the code to the top of my Main form Form_Load. it generates a RunTime Error 91. The first instance runs OK, it is only when I try and execute the program the second time that it pops the RunTime Error 91.
If there is a way to LOCK my program on the screen, so that the user can not move it, the problem becomes mute.
Aug 25th, 2006, 11:59 AM
#18
Re: couple of simple questions
Originally Posted by
JohnDonaldson
I first added the code to the top of my Main form Form_Load. it generates a RunTime Error 91....
Sorry, but that's not possible. Make an exe from the attached form and you'll only be able to get one instance.
Attached Files
Aug 25th, 2006, 11:59 AM
#19
Re: couple of simple questions
both the methods suggest (Form_Load or Sub Main) will work - we'd need to see code to see why they're not working for you.
To stop the form being moved: You can set Moveable = False at design time. Or check out Karl E. Peterson's FormBdr example to change it a runtime.
Aug 25th, 2006, 12:12 PM
#20
Thread Starter
Addicted Member
Re: couple of simple questions
I tried adding at the top of Form_Load
VB Code:
Private Sub Form_Load
Me.Movable = False
It generates this message error
"Function or interface marked as restricted, or function uses an automation type not supported by Visual Basic."
Aug 25th, 2006, 12:15 PM
#21
Re: couple of simple questions
Aug 25th, 2006, 12:16 PM
#22
Re: couple of simple questions
Originally Posted by
bushmobile
To stop the form being moved: You can set Moveable = False at
design time . Or check out Karl E. Peterson's
FormBdr example to change it a runtime.
....
Aug 25th, 2006, 12:17 PM
#23
Thread Starter
Addicted Member
Re: couple of simple questions
Hack.....Just the Form_Load or the entire Form. The entire Form is too big to post here.
Aug 25th, 2006, 12:19 PM
#24
Re: couple of simple questions
Originally Posted by
JohnDonaldson
Hack.....Just the Form_Load or the entire Form. The entire Form is too big to post here.
Attach the form. If it's too big then put it in a zip file.
Aug 25th, 2006, 12:25 PM
#25
Thread Starter
Addicted Member
Re: couple of simple questions
I am allowed to post the Form_Load but not the entire Form. IP reasons.
VB Code:
Private Sub Form_Load()
Dim conPath As String
'conPath = "Data Source=" & App.Path & "\PatientFiles2.mdb;"
'conPath = "Data Source=" & "D:" & "\PatientFiles2.mdb;"
conPath = "Data Source=" & "C:" & "\PatientFiles2.mdb;"
Set objAccessConnection = New ADODB.Connection
Set rsAccess = New ADODB.Recordset
'open connection to server
objAccessConnection.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;" & _
conPath & "Jet OLEDB:Database Password="
'check connection status
If objAccessConnection.State = adStateOpen Then
'MsgBox "Connection open!", 64, "Connection status."
connectionopen = True
Else
MsgBox "Connection not possible.", 64, "Connection status."
connectionopen = False
End If
txtDate.Text = Date
txtTime.Text = Time()
'Connect and setup JoyStick
Dim rt As Long
Dim JoyTestInfo As JOYINFO
Dim JoyTestInfo1 As JOYINFO
Dim JoyStickCaps As JOYCAPS
Dim JoyStickCaps1 As JOYCAPS
'Get the max and min position on the joystick
joyGetDevCaps JOYSTICK1, JoyStickCaps, Len(JoyStickCaps)
With JoyStickCaps
MaxX = .wXmax
MinX = .wXmin
MaxY = .wYmax
MinY = .wYmin
End With
joyGetDevCaps JOYSTICK2, JoyStickCaps1, Len(JoyStickCaps1)
With JoyStickCaps1
MaxX = .wXmax
MinX = .wXmin
MaxY = .wYmax
MinY = .wYmin
End With
StopButton1 = True
'RootPathName = "D:\"
RootPathName = "C:\"
'get the drive's disk parameters
Call GetDiskFreeSpaceEx(RootPathName, _
BytesFreeToCaller, _
TotalBytes, _
TotalFreeBytes)
'Read INI file
sIniFile = App.Path & "\perkins.ini"
sSection = "PlaySnd"
Call LoadIniFile
sSection = "FootSwitch"
Call LoadIniFile
frmIntializing.lblNew.Caption = " "
ShowPatients
VideoCapture = False
'Blank out Icons if no Patients in Database
If rsAccess.BOF Or rsAccess.EOF Then
'No more records
Image_Delete.Visible = False
Image_Update.Visible = False
Image_Capture.Visible = False
Image_Play.Visible = False
Image_DVD.Visible = False
End If
Disk80P = -1
Disk85P = -1
Me.FrameMenu.BackColor = &HFFECCC
Me.lblDate.BackColor = &HFFECCC
Me.lblTime.BackColor = &HFFECCC
Me.lblDiskSpace.BackColor = &HFFECCC
Me.lblPercent.BackColor = &HFFECCC
Me.Timer2.Enabled = False
'set Datagrid to highligt row and disable row moving
With DataGrid1
.MarqueeStyle = dbgHighlightRow
.AllowRowSizing = False
End With
End Sub
Aug 25th, 2006, 12:34 PM
#26
Re: couple of simple questions
John if you put the PrevInstance code at the top of that and you get an error then something very strange is going on. Did you try the form I attached? If so, what happened when you did?
Aug 25th, 2006, 12:43 PM
#27
Thread Starter
Addicted Member
Re: couple of simple questions
MartinLess........Your form run OK.
Aug 25th, 2006, 12:48 PM
#28
Re: couple of simple questions
Then if you did the same thing in your project and it's still a problem I'm sorry but there is nothing else I can do for you without your project. If you can get permission you could PM me and I'll give you my email address so you could send it to me.
Aug 25th, 2006, 01:14 PM
#29
Thread Starter
Addicted Member
Re: couple of simple questions
Aug 25th, 2006, 02:00 PM
#30
PowerPoster
Re: couple of simple questions
Originally Posted by
JohnDonaldson
I tried adding at the top of Form_Load
VB Code:
Private Sub Form_Load
Me.Movable = False
me.MOVEABLE...notice the E there after the V and before the A? Anyway, set it at design time rather than in form_load...look up "moveable" and set it to false on the form...that should work (did for me)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
Aug 25th, 2006, 02:33 PM
#31
Thread Starter
Addicted Member
Re: couple of simple questions
Found a lot easier method. I just removed the title bar by setting "BorderSytle = 0". Then adding to Form_Load
VB Code:
With Me
.Top = 0
.Left = 0
End With
Now the user can not move the window and it is locked to 0,0.
Now to disable the Windows Key and ALT+Esc so that the user can not get to the desktop and I'll all set.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width