I'm having trouble inserting names with a single quote such as O'Reilly into a SQL Server table. I've tried using 3 double quotes around objItem.MailboxDisplayName and I still receive the same error message.
Error: Incorrect syntax near 'Reilly'
I've pasted my script below, can someone please help? Thanks
strComputer = "Exchange"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\ROOT\MicrosoftExchangeV2")
Set colItems = objWMIService.ExecQuery _
("Select * from Exchange_Mailbox WHERE LastLogonTime IS NOT NULL")
strComputerSQL = "SQLServer"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider=SQLOLEDB;Data Source=" & strComputerSQL & ";" & _
"Trusted_Connection=Yes;Initial Catalog=MailboxUsage"
For Each objItem in colItems
sql = "INSERT INTO MailboxUsage (MailboxName, LastLogonTime, LastLoggedOnBy) VALUES ('" _
& objItem.MailboxDisplayName & "','" & objItem.LastLogonTime & "' ,'" _
& objItem.LastLoggedOnUserAccount & "')"
objConnection.Execute(sql)
Next
Error: Incorrect syntax near 'Reilly'
I've pasted my script below, can someone please help? Thanks
strComputer = "Exchange"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\ROOT\MicrosoftExchangeV2")
Set colItems = objWMIService.ExecQuery _
("Select * from Exchange_Mailbox WHERE LastLogonTime IS NOT NULL")
strComputerSQL = "SQLServer"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider=SQLOLEDB;Data Source=" & strComputerSQL & ";" & _
"Trusted_Connection=Yes;Initial Catalog=MailboxUsage"
For Each objItem in colItems
sql = "INSERT INTO MailboxUsage (MailboxName, LastLogonTime, LastLoggedOnBy) VALUES ('" _
& objItem.MailboxDisplayName & "','" & objItem.LastLogonTime & "' ,'" _
& objItem.LastLoggedOnUserAccount & "')"
objConnection.Execute(sql)
Next