david gibson
asked on
I need help starting the sql server express service from excel vba
hi there, i'm using windows 10 and trying to start the sql server express service from excel vba without much joy, pls see the below hopefully you can help.
'Start Service
strServiceName = "MSSQL$SQLEXPRESS"
Set objWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\. \root\cimv 2")
Set colListOfServices = objWMIService.ExecQuery("S elect * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService In colListOfServices
objService.StartService() 'this line results in err mesg = compile error "expected = "
Next
'Start Service
strServiceName = "MSSQL$SQLEXPRESS"
Set objWMIService = GetObject("winmgmts:{imper
Set colListOfServices = objWMIService.ExecQuery("S
For Each objService In colListOfServices
objService.StartService() 'this line results in err mesg = compile error "expected = "
Next
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Peter has a point.
ASKER
Thanks Chaps, both gave a me a different way of thinking. Peter i tried the RC xl and RaA approach which didn't start the sql server service. However i think you make a good point to recommend against performing starting/stopping this type of service in Excel VBA, which has caused me to rethink and move away from restarting the service in VBA and move to a checking/msgbox approach in VBA.. if the state = "running" (tnx Gustav) and if not then msgbox asking the user the 'restart their computer' which from what i know/tested automatically starts the sql server service again. It shouldn't be required that often anyhow, its main purpose is there as a backup in case something unexpected goes wrong.
Thanks again chaps, your time and effort is much appreciated.
Thanks again chaps, your time and effort is much appreciated.
There are probably other ways to start services too, not just via WMI, but I wasn't able to find anything right now.
Worst case you could simply execute
Worst case you could simply execute
net start MSSQL$SQLEXPRESS
by launching that command from Visual Basic, that should definitely work and won't require a reboot. If that doesn't work then I can see if I can think of another way.
ASKER
The code executed without errors, except the sql express service has not started. (ie. the service status column is still blank and sql connection wont open via vba)
Bit stumped, any thoughts welcomed.