Results 1 to 31 of 31

Thread: couple of simple questions

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    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?

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    Added

    VB Code:
    1. If App.PrevInstance then
    2.    Unload me
    3.    End
    4. End If

    I get an error when I try and execute a second copy.

    RunTime Error 91
    Object Variable or with Variable not set

  4. #4

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: couple of simple questions

    i'd recommend using Sub Main instead:
    VB Code:
    1. ' In a module
    2. Private Sub Main()
    3.     If Not App.PrevInstance Then frmMain.Show
    4. End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    VB Code:
    1. Private Sub Form_Load
    2.  
    3.    If App.PrevInstance then
    4.       Unload Me
    5.       End
    6.    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.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: couple of simple questions

    Quote Originally Posted by bushmobile
    i'd recommend using Sub Main instead:
    VB Code:
    1. ' In a module
    2. Private Sub Main()
    3.     If Not App.PrevInstance Then frmMain.Show
    4. End Sub
    As long as the App.PrevInstance is the first thing in Form_Load the effect is the same.

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: couple of simple questions

    Quote Originally Posted by JohnDonaldson
    VB Code:
    1. Private Sub Form_Load
    2.  
    3.    If App.PrevInstance then
    4.       Unload Me
    5.       End
    6.    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?

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: couple of simple questions

    Quote 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.

  10. #10
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: couple of simple questions

    2. Try this ShellWait function (need one CommandButton):

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    4. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
    5. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    6.  
    7. Private Const INFINITE = &HFFFF
    8. Private Const SYNCHRONIZE = &H100000
    9. Private Const WAIT_TIMEOUT = &H102
    10.  
    11. Public Sub ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbMinimizedFocus, Optional bDoEvents As Boolean = False)
    12.     Dim dwProcessID As Long
    13.     Dim hProcess As Long
    14.    
    15.     dwProcessID = Shell(PathName, WindowStyle)
    16.    
    17.     If dwProcessID = 0 Then
    18.         Exit Sub
    19.     End If
    20.    
    21.     hProcess = OpenProcess(SYNCHRONIZE, False, dwProcessID)
    22.    
    23.     If hProcess = 0 Then
    24.         Exit Sub
    25.     End If
    26.    
    27.     If bDoEvents Then
    28.         Do While WaitForSingleObject(hProcess, 100) = WAIT_TIMEOUT
    29.             DoEvents
    30.         Loop
    31.     Else
    32.         WaitForSingleObject hProcess, INFINITE
    33.     End If
    34.    
    35.         CloseHandle hProcess
    36. End Sub
    37.  
    38. Private Sub Command1_Click()
    39.     ShellWait "notepad.exe", vbNormalFocus
    40.         MsgBox "Finished !"
    41.             'continue code...
    42. End Sub

  11. #11

  12. #12

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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

  14. #14

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    I already have a module1.bas , so I added the code to it. Now I get multi-instances.

  16. #16

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    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.

  18. #18
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: couple of simple questions

    Quote 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 Attached Files

  19. #19
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    I tried adding at the top of Form_Load

    VB Code:
    1. Private Sub Form_Load
    2.  
    3.      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."

  21. #21
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: couple of simple questions

    Attach your form.

  22. #22
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: couple of simple questions

    Quote 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.
    ....

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    Hack.....Just the Form_Load or the entire Form. The entire Form is too big to post here.

  24. #24
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: couple of simple questions

    Quote 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.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    I am allowed to post the Form_Load but not the entire Form. IP reasons.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim conPath As String
    3.        
    4.     'conPath = "Data Source=" & App.Path & "\PatientFiles2.mdb;"
    5.     'conPath = "Data Source=" & "D:" & "\PatientFiles2.mdb;"
    6.     conPath = "Data Source=" & "C:" & "\PatientFiles2.mdb;"
    7.    
    8.     Set objAccessConnection = New ADODB.Connection
    9.     Set rsAccess = New ADODB.Recordset
    10.              
    11.     'open connection to server
    12.     objAccessConnection.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;" & _
    13.     conPath & "Jet OLEDB:Database Password="
    14.    
    15.     'check connection status
    16.     If objAccessConnection.State = adStateOpen Then
    17.         'MsgBox "Connection open!", 64, "Connection status."
    18.         connectionopen = True
    19.     Else
    20.         MsgBox "Connection not possible.", 64, "Connection status."
    21.         connectionopen = False
    22.     End If
    23.    
    24.     txtDate.Text = Date
    25.     txtTime.Text = Time()
    26.    
    27.    
    28.     'Connect and setup JoyStick
    29.     Dim rt As Long
    30.     Dim JoyTestInfo As JOYINFO
    31.     Dim JoyTestInfo1 As JOYINFO
    32.     Dim JoyStickCaps As JOYCAPS
    33.     Dim JoyStickCaps1 As JOYCAPS
    34.  
    35.     'Get the max and min position on the joystick
    36.    
    37.     joyGetDevCaps JOYSTICK1, JoyStickCaps, Len(JoyStickCaps)
    38.     With JoyStickCaps
    39.         MaxX = .wXmax
    40.         MinX = .wXmin
    41.         MaxY = .wYmax
    42.         MinY = .wYmin
    43.     End With
    44.    
    45.     joyGetDevCaps JOYSTICK2, JoyStickCaps1, Len(JoyStickCaps1)
    46.     With JoyStickCaps1
    47.         MaxX = .wXmax
    48.         MinX = .wXmin
    49.         MaxY = .wYmax
    50.         MinY = .wYmin
    51.     End With
    52.    
    53.     StopButton1 = True
    54.     'RootPathName = "D:\"
    55.     RootPathName = "C:\"
    56.     'get the drive's disk parameters
    57.     Call GetDiskFreeSpaceEx(RootPathName, _
    58.                             BytesFreeToCaller, _
    59.                             TotalBytes, _
    60.                             TotalFreeBytes)
    61.    
    62.    
    63.     'Read INI file
    64.    sIniFile = App.Path & "\perkins.ini"
    65.    
    66.    sSection = "PlaySnd"
    67.    Call LoadIniFile
    68.    sSection = "FootSwitch"
    69.    Call LoadIniFile
    70.    
    71.    frmIntializing.lblNew.Caption = " "
    72.     ShowPatients
    73.    
    74.    VideoCapture = False
    75.    
    76.     'Blank out Icons if no Patients in Database
    77.     If rsAccess.BOF Or rsAccess.EOF Then
    78.         'No more records
    79.         Image_Delete.Visible = False
    80.         Image_Update.Visible = False
    81.         Image_Capture.Visible = False
    82.         Image_Play.Visible = False
    83.         Image_DVD.Visible = False
    84.     End If
    85.    
    86.     Disk80P = -1
    87.     Disk85P = -1
    88.     Me.FrameMenu.BackColor = &HFFECCC
    89.     Me.lblDate.BackColor = &HFFECCC
    90.     Me.lblTime.BackColor = &HFFECCC
    91.     Me.lblDiskSpace.BackColor = &HFFECCC
    92.     Me.lblPercent.BackColor = &HFFECCC
    93.     Me.Timer2.Enabled = False
    94.    
    95.     'set Datagrid to highligt row and disable row moving
    96.     With DataGrid1
    97.         .MarqueeStyle = dbgHighlightRow
    98.         .AllowRowSizing = False
    99.     End With
    100.    
    101. End Sub

  26. #26
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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?

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    MartinLess........Your form run OK.

  28. #28
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    Re: couple of simple questions

    Thanks anyway.

  30. #30
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: couple of simple questions

    Quote Originally Posted by JohnDonaldson
    I tried adding at the top of Form_Load

    VB Code:
    1. Private Sub Form_Load
    2.  
    3.      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.

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    174

    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:
    1. With Me
    2.     .Top = 0
    3.     .Left = 0
    4. 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
  •  



Click Here to Expand Forum to Full Width