R
renee
I trying to use an ODBC connection and insert data into a database. I
got a script for excel help with I'm trying to modify to my uses.
The scprit call out :
Dim wrkODBC As Workspace
Dim cnWERP As Connection
Dim rsLIMS As Recordset
But VBA dose not recognize, Workspace, Connection, or Recordset -
Compile error: User-defined type not defined.
How do I define these??
Sub insertdata() 'soruceDBkey, DAILY_DATE, CODE, VALUE)
'test cases
Dim ws As Worksheet
Dim DBkey As String
Dim DAILYDATE() As String
Dim CODE() As String
Dim VALUE() As String
Dim sqlStmt As String
'Dim sqlstmtdelete As String
Dim connectionStr As String
Dim wrkODBC As Workspace
Dim cnWERP As ODBC.Connection
Dim rsLIMS As ODBC.Recordset
Set ws = Sheets("G211_C")
PrefDATEr = Range("F65000").End(xlUp).Row
soruceDATEr = Range("b65000").End(xlUp).Row
If soruceDATEr > PrefDATEr Then
DBkey = ws.Range("f6")
DAILYDATE = ws.Range("B" & PrefDATEr & ":B" & soruceDATEr)
CODE = ws.Range("C" & PrefDATEr & ":C" & soruceDATEr)
VALUE = ws.Range("D" & PrefDATEr & "" & soruceDATEr)
End If
'Setup WERP Database
Set wrkODBC = CreateWorkspace("WERPworkspace", _
"admin", "", dbUseODBC)
'Set wrkODBC = CreateWorkspace("WERPworkspace", _
' "admin", "", dbUseODBC)
'open connectionobject supplied informatio in the connect string
connectionStr = "ODBC;DATABASE=WRED;UID=pub;PWD=pub;DSN=WRED"
Set cnWRED = wrkODBC.OpenConnection("DBHYDRO", _
dbDriverNoPrompt, , _
connectionStr)
wrkODBC.BeginTrans
'need testing added here
sqlStmt = "insert into DM_DAILY_DATA(DBKEY, DAILY_DATE, CODE, VALUE)
values ('" & DBkey & "','" & DAILYDATE & "','" & CODE & "','" & VALUE &
"')"
cnWERP.Execute (sqlStmt)
wrkODBC.commitTrans
cnWRED.Close
'End
ErrHandler
wrok.rollback
cnWRED.Close
response = MsgBox(errMsg, vbCritical, "Error")
End Sub
Function Validate_Field(cnWRED As ODBC.Connection, sqlStmt As String)
As Boolean
Dim rs As Recordset
Dim status As Boolean
Set rs = cnWRED.openRecordset(sqlStmt)
If rs.fields(0) > 0 Then
status True
Else
status = False
End If
Validate_Fiels = status
End Function
got a script for excel help with I'm trying to modify to my uses.
The scprit call out :
Dim wrkODBC As Workspace
Dim cnWERP As Connection
Dim rsLIMS As Recordset
But VBA dose not recognize, Workspace, Connection, or Recordset -
Compile error: User-defined type not defined.
How do I define these??
Sub insertdata() 'soruceDBkey, DAILY_DATE, CODE, VALUE)
'test cases
Dim ws As Worksheet
Dim DBkey As String
Dim DAILYDATE() As String
Dim CODE() As String
Dim VALUE() As String
Dim sqlStmt As String
'Dim sqlstmtdelete As String
Dim connectionStr As String
Dim wrkODBC As Workspace
Dim cnWERP As ODBC.Connection
Dim rsLIMS As ODBC.Recordset
Set ws = Sheets("G211_C")
PrefDATEr = Range("F65000").End(xlUp).Row
soruceDATEr = Range("b65000").End(xlUp).Row
If soruceDATEr > PrefDATEr Then
DBkey = ws.Range("f6")
DAILYDATE = ws.Range("B" & PrefDATEr & ":B" & soruceDATEr)
CODE = ws.Range("C" & PrefDATEr & ":C" & soruceDATEr)
VALUE = ws.Range("D" & PrefDATEr & "" & soruceDATEr)
End If
'Setup WERP Database
Set wrkODBC = CreateWorkspace("WERPworkspace", _
"admin", "", dbUseODBC)
'Set wrkODBC = CreateWorkspace("WERPworkspace", _
' "admin", "", dbUseODBC)
'open connectionobject supplied informatio in the connect string
connectionStr = "ODBC;DATABASE=WRED;UID=pub;PWD=pub;DSN=WRED"
Set cnWRED = wrkODBC.OpenConnection("DBHYDRO", _
dbDriverNoPrompt, , _
connectionStr)
wrkODBC.BeginTrans
'need testing added here
sqlStmt = "insert into DM_DAILY_DATA(DBKEY, DAILY_DATE, CODE, VALUE)
values ('" & DBkey & "','" & DAILYDATE & "','" & CODE & "','" & VALUE &
"')"
cnWERP.Execute (sqlStmt)
wrkODBC.commitTrans
cnWRED.Close
'End
ErrHandler
wrok.rollback
cnWRED.Close
response = MsgBox(errMsg, vbCritical, "Error")
End Sub
Function Validate_Field(cnWRED As ODBC.Connection, sqlStmt As String)
As Boolean
Dim rs As Recordset
Dim status As Boolean
Set rs = cnWRED.openRecordset(sqlStmt)
If rs.fields(0) > 0 Then
status True
Else
status = False
End If
Validate_Fiels = status
End Function