Help converting vba to vbs

Discussion in 'Software' started by snyperj, Sep 27, 2011.

  1. snyperj

    snyperj Private E-2

    The below vba code opens an Access database by calling the Access Run Time.

    This is exactly what I need to do, but need to do it from vb script?

    Can anyone help by converting the below code so it works in vbs?

    Code:
    Sub OpenRunTime()
         Dim accpath As String, dbpath As String
         On Error Resume Next
         dbpath = "C:\My Application\MyApp.mdb"
         Set objAccess = GetObject(dbpath)
         If Err <> 0 Then
           If Dir(dbpath) = "" Then 'dbpath is not valid
             MsgBox "Couldn't find database."
             Exit Sub
           Else  'The full version of Microsoft Access is not installed.
             accpath = "C:\Program Files\Common Files\Microsoft Shared" & _
                  "\Microsoft Access Runtime\MSAccess.exe"
             If Dir(accpath) = "" Then
                MsgBox "Couldn't find Microsoft Access."
                Exit Sub
             Else
                Shell pathname:=accpath & " " & Chr(34) & dbpath & Chr(34), _
                  windowstyle:=6
                Do 'Wait for shelled process to finish
                  Err = 0
                  Set objAccess = GetObject(dbpath)
                Loop While Err <> 0
             End If
           End If
         End If
       End Sub
     
  2. GermanOne

    GermanOne Guest

    Untested:
    Code:
    OpenRunTime
    
    Sub OpenRunTime()
      dbpath = "C:\My Application\MyApp.mdb"
      accpath = "C:\Program Files\Common Files\Microsoft Shared\Microsoft Access Runtime\MSAccess.exe"
    
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objWSH = CreateObject("WScript.Shell")
    
      On Error Resume Next
      Set objAccess = GetObject(dbpath)
      If Err Then
        If Not ojFSO.FileExists(dbpath) Then 'dbpath is not valid
          MsgBox "Couldn't find database."
          Exit Sub
        Else  'The full version of Microsoft Access is not installed.
          If Not ojFSO.FileExists(accpath) Then
            MsgBox "Couldn't find Microsoft Access."
            Exit Sub
          Else
            objWSH.Run """" & accpath & """ """ & dbpath & """", 6, True
          End If
        End If
      End If
    End Sub
    
    Some issues:
    What happen if the GetObject function was successful and what about the referenced data base object (objAccess) in this case?

    Regards
    GermanOne
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds