Results 1 to 5 of 5

Thread: I want to improve my CD-ROM detector...please help....

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Hi.

    Put this code in a form1's form load event, and add a label1.

    When you start the program it looks for a CD called stuff.exe in the root of the CD-ROM drive. If the CD is in there, the program loads as normal, by loading form2.

    If there is no CD with stuff.exe, a message box comes up: 'Please insert the appropriate CD-ROM in the drive.'.

    Then when 'OK' is clicked, the message box disappears. However, the program is still in memory when 'Ok' is clicked. (I just set the visible property of Form1 to false). I thought the 'Exit Sub' whould take care of this, but it doesn't.

    Could someone please look at my code and tell me how to unload Form1 when the user clicks 'OK' on the message box?

    Thanks for your help.

    ---------------


    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    Private Const DRIVE_REMOVABLE = 2
    Private Const DRIVE_FIXED = 3
    Private Const DRIVE_REMOTE = 4
    Private Const DRIVE_CDROM = 5
    Private Const DRIVE_RAMDISK = 6

    'Set the form's visible property to false
    Private Sub Form_Load()
    Dim TheFile As String
    Dim Results As String
    'the program detects the CD drive. When it finds it, it looks
    'for a file on the CD, in this case "stuff
    '.exe". When it finds it, the program loads as normal. If it's
    'not there, a message box pops up, asking them to install the CD
    Dim r&, allDrives$, JustOneDrive$, pos%, DriveType&
    Dim CDfound As Integer
    allDrives$ = Space$(64)
    r& = GetLogicalDriveStrings(Len(allDrives$), allDrives$)
    allDrives$ = Left$(allDrives$, r&)
    Do
    pos% = InStr(allDrives$, Chr$(0))
    If pos% Then
    JustOneDrive$ = Left$(allDrives$, pos%)
    allDrives$ = Mid$(allDrives$, pos% + 1, Len(allDrives$))
    DriveType& = GetDriveType(JustOneDrive$)
    If DriveType& = DRIVE_CDROM Then
    CDfound% = True
    Exit Do
    End If
    End If
    Loop Until allDrives$ = "" Or DriveType& = DRIVE_CDROM

    Label1 = UCase$(JustOneDrive$)
    TheFile = Label1.Caption & "stuff.exe"
    On Error GoTo errorhandler
    Results = Dir$(TheFile)
    If Results = "" Then
    Message = "Please insert the appropriate CD-ROM."
    buttonsandicons = vbOKOnly + vbExclamation
    Title = "Stuff"
    MsgBox Message, buttonsandicons, Title
    Exit Sub
    Else
    Form1.Show
    End If
    Exit Sub
    errorhandler:
    Message = "Please insert the Maths-Master CD-ROM."
    buttonsandicons = vbOKOnly + vbExclamation
    Title = "Stuff"
    MsgBox Message, buttonsandicons, Title
    Exit Sub
    End Sub










  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    For some reason a smiley turns out in the middle of my code.

    The line is supposed to be "allDrives$ = Left$(allDrives$, r&)"

  3. #3
    Guest
    switch

    Code:
    MsgBox Message, buttonsandicons, Title

    with


    Code:
    Dim iAnswer as Integer
    iAnswer = MsgBox (Message, buttonsandicons, Title)
    If iAnswer = vbOk Then Unload Me 'Or try 'End'
    i havent had enough time to look at your code but i think this will do

    ~~~~~~~~~~
    ~~Chenko~~
    ~~~~~~~~~~

  4. #4
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    Fixed code
    Code:
    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    Private Const DRIVE_REMOVABLE = 2
    Private Const DRIVE_FIXED = 3
    Private Const DRIVE_REMOTE = 4
    Private Const DRIVE_CDROM = 5
    Private Const DRIVE_RAMDISK = 6
    
    'Set the form's visible property to false
    Private Sub Form_Load()
        Dim TheFile As String
        Dim Results As String
        Dim intReturn As Integer '/////////////New declaration
        'the program detects the CD drive. When it finds it, it looks
        'for a file on the CD, in this case "stuff
        '.exe". When it finds it, the program loads as normal. If it's
        'not there, a message box pops up, asking them to install the CD
        Dim r&, allDrives$, JustOneDrive$, pos%, DriveType&
        Dim CDfound As Integer
        allDrives$ = Space$(64)
        r& = GetLogicalDriveStrings(Len(allDrives$), allDrives$)
        allDrives$ = Left$(allDrives$, r&)
        Do
            pos% = InStr(allDrives$, Chr$(0))
            If pos% Then
                JustOneDrive$ = Left$(allDrives$, pos%)
                allDrives$ = Mid$(allDrives$, pos% + 1, Len(allDrives$))
                DriveType& = GetDriveType(JustOneDrive$)
                If DriveType& = DRIVE_CDROM Then
                    CDfound% = True
                    Exit Do
                End If
            End If
        Loop Until allDrives$ = "" Or DriveType& = DRIVE_CDROM
        
        Label1 = UCase$(JustOneDrive$)
        TheFile = Label1.Caption & "stuff.exe"
        On Error GoTo errorhandler
        Results = Dir$(TheFile)
        If Results = "" Then
            Message = "Please insert the appropriate CD-ROM."
            buttonsandicons = vbOKOnly + vbExclamation
            Title = "Stuff"
            '/////////////Changed code
            intReturn = MsgBox(Message, buttonsandicons, Title)
            If intReturn = vbOK Then
                Unload Me
            End If
            '/////////////
            Exit Sub
        Else
            Form1.Show
        End If
        Exit Sub
    errorhandler:
        Message = "Please insert the Maths-Master CD-ROM."
        buttonsandicons = vbOKOnly + vbExclamation
        Title = "Stuff"
        MsgBox Message, buttonsandicons, Title
        Exit Sub
    End Sub
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Thumbs up


    Thanks lads, I've got it sorted now!


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