Link to home
Start Free TrialLog in
Avatar of david gibson
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:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService In colListOfServices
objService.StartService()      'this line results in err mesg = compile error "expected = "
Next
SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of david gibson
david gibson

ASKER

Thanks for the reply Gustav :-)
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Peter has a point.
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.
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
net start MSSQL$SQLEXPRESS

Open in new window

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.