저는 (이번 아침와 같이) vba를 처음 접했기 때문에 이것은 바보 같은 실수 일 수 있습니다. ADODB를 사용하여 데이터베이스에서 정보를 가져오고 연결된 양식을 정보로 자동 채 웁니다. 내 기본 설정은 다음과 같습니다 :vba에서 ADODB를 사용하여 연속 DB 쿼리 실행
Private Sub BuildButton_Click()
'Declare Variables'
Dim con
'Connect to the Database'
Set con = CreateObject("ADODB.Connection")
With con
.ConnectionString = "DRIVER=SQLServer;SERVER=...;database=...;"
End With
con.Open
Set result = CreateObject("ADODB.Recordset")
'Defining Query Keys'
Family = "'" & FamilyBox.Value & "'"
Rating = "'" & RatingBox.Value & "'"
'SQL Queries'
Query1 = "SELECT ... "
Query2 = "SELECT ... "
'Query #1'
result.Open Query1, con
Do Until result.EOF
Range("D4").Value = Tier(result.Fields(0).Value)
result.MoveNext
Loop
'Query #2'
result.Open Query2, con
Do Until result.EOF
Range("D6").Value = result.Fields(0).Value
Range("H9").Value = result.Fields(1).Value
Range("H8").Value = Contact(result.Fields(1).Value)
Loop
그것은 첫째 쿼리를 통해 잘 작동하지만, 말하는 오류를주는 "result.Open QUERY2, 사기꾼"라인에 잡힐 :
Run-time error '3705'
Operation is not allowed when the object is open.
가 만드는 나에게 감각이 있지만 올바른 방법을 알아내는 데 어려움을 겪고 있습니다. 내가 해봤
다른 것들 :
Compile error:
Expected Sub, Function, or Property
추가 : 오류를 준다 "result.Open QUERY2, 사기꾼"에서 "결과 QUERY2, 사기꾼 변경"
, line "result.Close"를 두 쿼리 사이에서 다시 열 수 있습니다. 이 프로그램을 충돌하고 오류를 얻을 : 그것은 간단한 수정해야처럼
이Run-time error '-2147417848 (80010108)': Method 'Value' of object 'Range' failed
것 같습니다, 난 그냥 붙어 왔. 어떤 도움을 주셔서 감사합니다.
업데이트 코멘트에 말했듯이, 나는 단순히 두 번째 루프에서 result.MoveNext를 잊어 열립니다 사이의 result.Close 명령을 추가와 함께
Option Explicit
Private Sub BuildButton_Click()
'Declare Variables'
Dim con As ADODB.Connection, result As ADODB.Recordset
Dim Family As String, Rating As String, Query1 As String, Query2 As String
'Connect to the Database'
Set con = CreateObject("ADODB.Connection")
With con
.ConnectionString = "DRIVER=SQLServer;SERVER=...;database=...;"
End With
con.Open
Set result = CreateObject("ADODB.Recordset")
'Defining Query Keys'
Family = "'" & FamilyBox.Value & "'"
Rating = "'" & RatingBox.Value & "'"
'SQL Queries'
Query1 = "SELECT ... "
Query2 = "SELECT ... "
'Query #1'
result.Open Query1, con
Do Until result.EOF
Range("D4").Value = Tier(result.Fields(0).Value)
result.MoveNext
Loop
result.Close
Set result = CreateObject("ADODB.Recordset")
'Query #2'
result.Open Query2, con
Do Until result.EOF
Range("D6").Value = result.Fields(0).Value
Range("H9").Value = result.Fields(1).Value
Range("H8").Value = Contact(result.Fields(1).Value)
Loop
당신이 또한 포함나요 '설정 결과 = CreateObject를 ("ADODB.Recordset")를'닫기 후 ,하지만 열기 전에? 또한 변수를 반드시 유형으로 희석하고 싶지 않은 이유는 무엇입니까? –
나는하지는 않았지만 방금 시도한 결과와 같은 오류가 발생했습니다. –
실제 코드 (마이너스 비밀번호 등)를 게시 할 수 있습니까? 때로는 오타가 있습니다. 또한 OPTION EXPLICIT을 제안합니다. 이 코드를 어디서 실행하고 있습니까? Excel? –