Solved

MSDE2000 Starting service in VB / Attaching Databases

Posted on 2002-07-24
3
302 Views
Last Modified: 2012-08-13
I am building a VB application using MSDE as an engine.  Originally this application was built using MSDE 1.0 (SQL 7.0).  I found examples of how to start MSDE if it wasn't already started.

Now I have moved to MSDE2000.  The same code that started MSDE 1.0 now does not work. It gives an error [SQL-DMO]Service Control Error:  The specified service does not exist as an installed service.

Server name is (local) since the application is a workstation based application.

If I use the sample code from:

c:\program files\microsoft SQL server\80\tools\devtools\samples\sqldmo\vb\service

I get the same error.

This is really upsetting because I lost a huge sale last December because I couldn't find an answer to this message doing a demo at a client site on a client PC running Windows 2000.

Why provide an example of how to start the service if the example does not work!

Basically I need to make sure MSDE is started on the workstation, check to see if a database exists, if not, attach the database, set defaults in tables etc.


--
Dave Edwinson
Apps Done Rite
     "Building the next steps"
www.geocities.com/dedwinson
0
Comment
Question by:edwinson
3 Comments
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
as simple hotfix you can try to use net start 'your service name'
and wait for 3-4 seconds to start this service.
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 100 total points
Comment Utility
I found some useful code here answered by TimCottee:

Get the Status of a service: http://www.experts-exchange.com/visualbasic/Q_20320650.html

Public Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long

Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SPECIFIC_RIGHTS_ALL = &HFFFF

' Service database names
Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Const SERVICES_FAILED_DATABASE = "ServicesFailed"

' Value to indicate no change to an optional parameter
Const SERVICE_NO_CHANGE = &HFFFF

' Service State -- for Enum Requests (Bit Mask)
Const SERVICE_ACTIVE = &H1
Const SERVICE_INACTIVE = &H2
Const SERVICE_STATE_ALL = (SERVICE_ACTIVE Or SERVICE_INACTIVE)

' Controls
Const SERVICE_CONTROL_STOP = &H1
Const SERVICE_CONTROL_PAUSE = &H2
Const SERVICE_CONTROL_CONTINUE = &H3
Const SERVICE_CONTROL_INTERROGATE = &H4
Const SERVICE_CONTROL_SHUTDOWN = &H5

' Service State -- for CurrentState
Const SERVICE_STOPPED = &H1
Const SERVICE_START_PENDING = &H2
Const SERVICE_STOP_PENDING = &H3
Const SERVICE_RUNNING = &H4
Const SERVICE_CONTINUE_PENDING = &H5
Const SERVICE_PAUSE_PENDING = &H6
Const SERVICE_PAUSED = &H7

' Controls Accepted  (Bit Mask)
Const SERVICE_ACCEPT_STOP = &H1
Const SERVICE_ACCEPT_PAUSE_CONTINUE = &H2
Const SERVICE_ACCEPT_SHUTDOWN = &H4

' Service Control Manager object specific access types
Const SC_MANAGER_CONNECT = &H1
Const SC_MANAGER_CREATE_SERVICE = &H2
Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Const SC_MANAGER_LOCK = &H8
Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20

Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)

' Service object specific access type
Const SERVICE_QUERY_CONFIG = &H1
Const SERVICE_CHANGE_CONFIG = &H2
Const SERVICE_QUERY_STATUS = &H4
Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Const SERVICE_START = &H10
Const SERVICE_STOP = &H20
Const SERVICE_PAUSE_CONTINUE = &H40
Const SERVICE_INTERROGATE = &H80
Const SERVICE_USER_DEFINED_CONTROL = &H100
Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL)

Public Type SERVICE_STATUS
   dwServiceType As Long
   dwCurrentState As Long
   dwControlsAccepted As Long
   dwWin32ExitCode As Long
   dwServiceSpecificExitCode As Long
   dwCheckPoint As Long
   dwWaitHint As Long
End Type

Public Function QueryService(strServiceName As String, strServerName As String) As String
   App.LogEvent "QueryService : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   lngSMHandle = OpenSCManager(strServerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
   lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
   ret = QueryServiceStatus(lngSvcHandle, ssStatus)
   If lngSvcHandle = 0& Then QueryService = "Service Not Found"
   Select Case ssStatus.dwCurrentState
   Case SERVICE_STOPPED
       QueryService = "Service Is Stopped"
   Case SERVICE_START_PENDING
       QueryService = "Service Start Pending"
   Case SERVICE_STOP_PENDING
       QueryService = "Service Stop Pending"
   Case SERVICE_RUNNING
       QueryService = "Service Is Running"
   Case SERVICE_CONTINUE_PENDING
       QueryService = "Service Continue Pending"
   Case SERVICE_PAUSE_PENDING
       QueryService = "Service Pause Pending"
   Case SERVICE_PAUSED
       QueryService = "Service Paused"
   End Select
   CloseServiceHandle lngSvcHandle
   CloseServiceHandle lngSMHandle
End Function

Public Sub StopSvc(strServiceName As String, strServerName As String)
   App.LogEvent "Stop Service : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   lngSMHandle = OpenSCManager(strServerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
   lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
   ret = QueryServiceStatus(lngSvcHandle, ssStatus)
   If lngSvcHandle <> 0& Then
       ControlService lngSvcHandle, SERVICE_CONTROL_STOP, ssStatus
   End If
   CloseServiceHandle lngSvcHandle
   CloseServiceHandle lngSMHandle
End Sub

Public Sub StartSvc(strServiceName As String, strServerName As String)
   App.LogEvent "Start Service : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   lngSMHandle = OpenSCManager(strServerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
   lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
   ret = QueryServiceStatus(lngSvcHandle, ssStatus)
   If lngSvcHandle <> 0& Then
       StartService lngSvcHandle, 0&, 0&
   End If
   CloseServiceHandle lngSvcHandle
   CloseServiceHandle lngSMHandle
End Sub

Hope this make helps
0
 
LVL 1

Author Comment

by:edwinson
Comment Utility
A bit more messy than I actually did, but it worked great.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now