Good Morning Dear Masters,
I created a code to get data from the server. It worked very well over a year. However, somehow one of the criteria has been changed about "refused name(no name)" customers. Before we used to assign customer number for them automatically, but with the new changes the system does not assign any customer number customer numbers for those customer instead it gives them "R/N" code. Therefore now I have some problem extracting data from server. I tried to apply some of those "by-pass techniques that I found on net but they did not work for me (talking about CAST).
I get this error:
All I need is disregard those "R/N" in customer id field. Please have a look at my vba code below.
Thank you in advance
Baha
I created a code to get data from the server. It worked very well over a year. However, somehow one of the criteria has been changed about "refused name(no name)" customers. Before we used to assign customer number for them automatically, but with the new changes the system does not assign any customer number customer numbers for those customer instead it gives them "R/N" code. Therefore now I have some problem extracting data from server. I tried to apply some of those "by-pass techniques that I found on net but they did not work for me (talking about CAST).
Code:
case WHEN isnumeric([CustId]) THEN CAST([CustId] AS int) else 0 end
I get this error:
PHP:
Run-time error ' -2147217913 (80040e07)
[Microsoft] [ODBC SQL Server Driver][SQL Server] Conversation failed
when converting the nvarchar value 'R/N'to data type int.
All I need is disregard those "R/N" in customer id field. Please have a look at my vba code below.
Thank you in advance
Baha
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim DBFullName, TableName As String
Dim TargetRange As Range
Dim Conn As ADODB.Connection, intColIndex As Integer
Dim cel As Range
Dim TD As Double
Dim TD2 As Double
Dim qdate As Double
Dim qdate2 As Double
Dim lastrow As Long
Dim custid 'As String+
TD = DateSerial(Year(Now()), Month(Now()), 1)
TD2 = Target.Offset(-4, -2).Value
custid = Range("custid4").Text
Target.Cells(1, 1).Select
Selection.Activate
Set TargetRange = Range("custid4").Offset(2, 0)
Set Conn = New ADODB.Connection
Conn.Open "driver={SQL Server};" & _
"server=XXXX01\RPTDB;database=Reporting_ODS;"
Set RecSet = New Recordset
RecSet.Open "SELECT TOP(1)Baha_PM.Lname +' '+ Baha_PM.Fname FROM Reporting_ODS.TG.Baha_PM Baha_PM " & _
"WHERE Baha_PM.CustId='" & custid & "'", Conn, , , adCmdText
TargetRange.CopyFromRecordset RecSet
RecSet.Close
Set RecSet = Nothing
Conn.Close
' Trip
Set TargetRange = Range("custid4").Offset(14, 0)
Set Conn = New ADODB.Connection
Conn.Open "driver={SQL Server};" & _
"server=XXXX01\RPTDB;database=Reporting_ODS;"
Set RecSet = New Recordset
RecSet.Open "SELECT MAX(Baha_PM.Trip_Id) FROM Reporting_ODS.TG.Baha_PM Baha_PM " & _
"WHERE Baha_PM.CustId='" & custid & "'", Conn, , , adCmdText
TargetRange.CopyFromRecordset RecSet
RecSet.Close
Set RecSet = Nothing
Conn.Close
Set Conn = Nothing
End Sub