Thank you for the helpful tips SSChampion. I do care about my code, very much so. It has been a rough year learning as I go. The company I work for is all over the place with different languages and servers.
I spend what free time I have taking online tutorials and this old brain of mine is absorbing like a sponge treated with water repellent. I am stuck in the middle of having to pull data from MySQL, Sql Server, Oracle, Mongo, Linux, and a whole host of random APIs via Soap.
Along with coders that like VB, Python, Perl and GoogleGo. It's all starting to mash together and I'm hitting a wall. I can't keep waking up with keyboard imprints on my forehead. But people keep dropping like flies over here and fear I may be soon.
I can hear the little violins playing, so I'll stop. I appreciate the add in recommendation for making the code look readable, but the company won't swing for the cost.
To answer your question, no, SAASUFFOLK is one database running on a Linux server that I view on my server through Oracle SQL Developer and or Microsoft SQL Server 2014 Management Studio.
I need to build macro enabled Excel sheets for departments that don't have Oracle installed on there machines. So I am stuck in VBA building CommandText with fingers crossed. Hence the long garbled query (I couldn't figure out the line continuation in VBA with a string).
Here is the full code minus server info. I don't know why this site replaced ":" with a smiley face.Public Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Adds windows spinning wheel while data is updating
Application.ScreenUpdating = True
Sheets("Accounts").Cells.Clear
Dim Server_IP As String
Dim User_ID As String
Dim Password As String
Server_IP = " xxxx "
User_ID = " xxxx "
Password = " xxxx "
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
Dim QueryEnd As String
'The completed Connection string will look like this objMyConn.ConnectionString = Provider=SQLOLEDB;Data Source= xxxxx ;UserID= roc;Password= xxxxx ;
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=" & Server_IP & ";User ID=" & User_ID & ";Password=" & Password & ";"
objMyConn.Open
Set objMyCmd.ActiveConnection = objMyConn
Dim AcctARRAY() As Variant
'Range where account numbers are entered in
AcctARRAY = Range("C6:C15")
For R = 1 To UBound(AcctARRAY, 1)
If R = 1 Then
QueryEnd = "WHERE(ACCOUNT_NUMBER=''" & AcctARRAY(R, 1) & "''"
Else
QueryEnd = QueryEnd & " OR ACCOUNT_NUMBER=''" & AcctARRAY(R, 1) & "''"
End If
Next R
QueryEnd = QueryEnd & ")"
'Declare varible to insert the ListBox selection in to the Command String
Dim Instance As String
Instance = Me.ListBox1.Value
objMyCmd.CommandText = "SELECT * FROM OPENQUERY(" & Instance & "," _
& "'SELECT bd.account_number,bd.equipment_mac, " _
& " SUM(cdf.SUM_BYTES_UP), " _
& " SUM(cdf.SUM_BYTES_DOWN), " _
& " SUM(cdf.SUM_RESET_COUNT), " _
& " AVG(cdf.AVG_TXPOWER_UP), " _
& " AVG(cdf.AVG_RXPOWER_DOWN), " _
& " AVG(cdf.AVG_RXPOWER_UP), " _
& " AVG(cdf.AVG_PATH_LOSS_UP), " _
& " MAX(cdf.MAX_CER_DOWN), " _
& " MAX(cdf.MAX_CCER_DOWN), " _
& " MIN(cdf.MIN_SNR_DOWN), " _
& " MAX(cdf.US_CER_MAX), " _
& " MAX(cdf.US_CCER_MAX), " _
& " MIN(cdf.US_SNR_MIN), " _
& " SUM(cdf.T3_TIMEOUTS), " _
& " SUM (cdf.T4_TIMEOUTS), " _
& " MAX(cdf.SYSUPTIME), " _
& " bd.first_name, " _
& " bd.last_name, " _
& " bd.street_number, " _
& " bd.street_name, " _
& " bd.city, " _
& " bd.custom_field_1, " _
& " bd.phone FROM billing_data bd JOIN billing_device_mapping bdm ON bdm.equipment_model = bd.equipment_model AND bdm.device_type = ''CM'' LEFT OUTER JOIN topology_node tn ON bd.equipment_mac = tn.macaddr LEFT OUTER JOIN cm_hour_facts cdf ON tn.topologyid = cdf.cm_id AND cdf.hour_stamp >= SYSDATE-1 " & QueryEnd & "GROUP BY EQUIPMENT_MAC,bd.account_number,bd.first_name,bd.last_name,bd.street_number,bd.street_name,bd.city,bd.custom_field_1,bd.phone')"
Debug.Print objMyCmd.CommandText
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open objMyCmd
'Declare Header Names
With Sheets("Accounts")
With .Cells
.Cells(1, 1).Value = "ACCOUNT NUMBER"
.Cells(1, 2).Value = "EQUIPMENT_MAC"
.Cells(1, 3).Value = "SUM_BYTES_UP"
.Cells(1, 4).Value = "SUM_BYTES_DOWN"
.Cells(1, 5).Value = "SUM_RESET_COUNT"
.Cells(1, 6).Value = "AVG_TXPOWER_UP"
.Cells(1, 7).Value = "AVG_RXPOWER_DOWN"
.Cells(1, 8).Value = "AVG_RXPOWER_UP"
.Cells(1, 9).Value = "AVG_PATH_LOSS_UP"
.Cells(1, 10).Value = "MAX_CER_DOWN"
.Cells(1, 11).Value = "MAX_CCER_DOWN"
.Cells(1, 12).Value = "MIN_SNR_DOWN"
.Cells(1, 13).Value = "US_CER_MAX"
.Cells(1, 14).Value = "US_CCER_MAX"
.Cells(1, 15).Value = "US_SNR_MIN"
.Cells(1, 16).Value = "SUM_T3_TIMEOUTS"
.Cells(1, 17).Value = "SUM_T4_TIMEOUTS"
.Cells(1, 18).Value = "MAX_SYSUPTIME"
.Cells(1, 19).Value = "FIRST_NAME"
.Cells(1, 20).Value = "LAST_NAME"
.Cells(1, 21).Value = "STREET_NUMBER"
.Cells(1, 22).Value = "STREET_NAME"
.Cells(1, 23).Value = "CITY"
.Cells(1, 24).Value = "DEVICE_TYPE"
.Cells(1, 25).Value = "PHONE"
End With
.Range("A2").CopyFromRecordset (objMyRecordset)
End With
'close sql connection
objMyConn.Close
'Switch to the Accounts Sheet
Sheets("Accounts").Activate
'Sort on Column A running out to column P down to row 100. 100 should be enough
With ActiveWorkbook.Worksheets("Accounts").Sort
.SetRange Range("A2:P100")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub