S
Sauron
Hi All,
I thought this was going to be nice and straightforward but i
certainly isn't... for me anyway!
What I am trying to do is build a SQL query using the data entered int
certain cells pass it to SQL and write it to a table, unfortunately
just can't seem to make it work.
I have searched and searched for information and have come up wit
nothing and I am really really stuck. I would be very grateful for som
help!
Thanks,
Sau
Code Follows...
*-*-*-*-*
Dim Dno As String
Dim PatFn As String
Dim PatSn As String
Dim SC1 As String
Dim SC2 As String
Dim SC3 As String
Dim MnQry As String
Dim FinishOff As String
Dim Con As ADODB.Connection
Dim RS As ADODB.Recordset
Dim TCell As Range
Dim DOB As String
Dim Col As Integer
Dno = Worksheets("Query").Range("B5").Value
PatFn = Worksheets("Query").Range("B8").Value
PatSn = Worksheets("Query").Range("B11").Value
DOB = Worksheets("Query").Range("B14").Value
SC1 = Worksheets("Query").Range("I8").Value
SC2 = Worksheets("Query").Range("I11").Value
SC3 = Worksheets("Query").Range("I14").Value
'Con.Provider = "SQLOLEDB.1"
'Con.ConnectionString
"DATABASE=concorde;SERVER=ecpsrv;UID=sa;PWD=password;"
Set Con = New ADODB.Connection
Con.ConnectionString = "DSN=ECPSRV"
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.StatusBar = "Writing data from ECPSRV..."
End With
Set RS = New ADODB.Recordset
With RS
If Dno = Not Null Then
MnQry = MnQry & " AND _Patient.MedicalRecordNumber = " & _
Dno
End If
If PatFn = Not Null Then
MnQry = MnQry & " AND _Patient.FirstName"
End If
If PatSn = Not Null Then
MnQry = MnQry & "AND _Patient.BirthDate"
End If
FinishOff = "Select" & "FROM"
If MnQry <> "" Then
FinishOff = FinishOff & "WHERE" & Mid$(MnQry, 5)
End If
End With
For Col = 0 To RS.Fields.Count - 1
Worksheets("Query").Range("B22").Offset(0, Col).Value
RS.Fields(Col).Name
Range("B22").Offset(1, 0).CopyFromRecordset RS
Set RS = Nothing
Con.Close
Set Con = Nothing
Next
With Application
.StatusBar = False
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
*-*-*-*-
I thought this was going to be nice and straightforward but i
certainly isn't... for me anyway!
What I am trying to do is build a SQL query using the data entered int
certain cells pass it to SQL and write it to a table, unfortunately
just can't seem to make it work.
I have searched and searched for information and have come up wit
nothing and I am really really stuck. I would be very grateful for som
help!
Thanks,
Sau
Code Follows...
*-*-*-*-*
Dim Dno As String
Dim PatFn As String
Dim PatSn As String
Dim SC1 As String
Dim SC2 As String
Dim SC3 As String
Dim MnQry As String
Dim FinishOff As String
Dim Con As ADODB.Connection
Dim RS As ADODB.Recordset
Dim TCell As Range
Dim DOB As String
Dim Col As Integer
Dno = Worksheets("Query").Range("B5").Value
PatFn = Worksheets("Query").Range("B8").Value
PatSn = Worksheets("Query").Range("B11").Value
DOB = Worksheets("Query").Range("B14").Value
SC1 = Worksheets("Query").Range("I8").Value
SC2 = Worksheets("Query").Range("I11").Value
SC3 = Worksheets("Query").Range("I14").Value
'Con.Provider = "SQLOLEDB.1"
'Con.ConnectionString
"DATABASE=concorde;SERVER=ecpsrv;UID=sa;PWD=password;"
Set Con = New ADODB.Connection
Con.ConnectionString = "DSN=ECPSRV"
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.StatusBar = "Writing data from ECPSRV..."
End With
Set RS = New ADODB.Recordset
With RS
If Dno = Not Null Then
MnQry = MnQry & " AND _Patient.MedicalRecordNumber = " & _
Dno
End If
If PatFn = Not Null Then
MnQry = MnQry & " AND _Patient.FirstName"
End If
If PatSn = Not Null Then
MnQry = MnQry & "AND _Patient.BirthDate"
End If
FinishOff = "Select" & "FROM"
If MnQry <> "" Then
FinishOff = FinishOff & "WHERE" & Mid$(MnQry, 5)
End If
End With
For Col = 0 To RS.Fields.Count - 1
Worksheets("Query").Range("B22").Offset(0, Col).Value
RS.Fields(Col).Name
Range("B22").Offset(1, 0).CopyFromRecordset RS
Set RS = Nothing
Con.Close
Set Con = Nothing
Next
With Application
.StatusBar = False
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
*-*-*-*-