MyTetra Share
Делитесь знаниями!
Подключение из Excel к Access через VBA
Время создания: 10.10.2019 07:05
Текстовые метки: Connect Excel To Access, ADO, Connection, Recordset, Excel-Access, CursorLocation, RecordCount
Раздел: Разные закладки - VBA - Access - ADO
Запись: xintrea/mytetra_db_adgaver_new/master/base/1570680318sot44eyn6o/text.html на raw.githubusercontent.com

Подключение из Excel к Access через VBA

Полезная функция по подключению из Excel к Access (предварительно надо подключить библиотеку MS ActiveX Data Objects 2.8 Library, как показано на картинке). Проверено - работает.

VBA Добавление Reference


Public Sub test_db()

ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & ActiveWorkbook.Path & "\Database4.accdb; Jet OLEDB:Database;"

Dim con As New ADODB.Connection

con.Open ConnectionString

On Error GoTo not_table

con.Execute ("SELECT TOP 1 * FROM Customers")

con.Close

Exit Sub


not_table:

con.Close

End Sub


https://coderoad.ru/30364090/%D0%A1%D1%87%D0%B5%D1%82%D1%87%D0%B8%D0%BA-%D0%BD%D0%B0%D0%B1%D0%BE%D1%80%D0%BE%D0%B2-%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D0%B5%D0%B9-%D0%B2%D1%81%D0%B5%D0%B3%D0%B4%D0%B0-%D0%B2%D0%BE%D0%B7%D0%B2%D1%80%D0%B0%D1%89%D0%B0%D0%B5%D1%82-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5-1

Вам нужно использовать статический курсор. Для этого вам нужно явно создать объект RecordSet, вот так:

Set objRS = Server.CreateObject("ADODB.Recordset")

objRS.CursorLocation = adUseClient

objRS.Open "SELECT * FROM " & viewName & ";", objConn, adOpenStatic,adLockReadOnly, adCmdText


Set objRS = Server.CreateObject("ADODB.Recordset")

objRS.CursorLocation = adUseClient

objRS.Open "SELECT * FROM " & viewName & ";", objConn, adOpenStatic,adLockReadOnly, adCmdText

Неявно созданные RecordSets имеют серверные курсоры, что приводит к возвращению значения -1.

Для получения дополнительной информации: http://www.adopenstatic.com/faq/recordcounterror.asp


http://www.adopenstatic.com/faq/recordcounterror.asp

NOTE: click here  to see superior alternatives to .RecordCount.

RecordCount returns -1
The use of the ADO Recordset's .RecordCount property requires either the use of:

  • Static or Keyset server-side cursors or
  • A client-side cursor (which returns a Static cursor)

(Note: some OLEDB Providers will return the correct recordcount with an adOpenDynamic cursor, others will not).

By default Recordsets are opened server-side, and with an adOpenForwardOnly cursor. Attempting to access the .RecordCount property with this type of cursor will return -1.

The easiest way to fix this is to change the cursor type to adOpenStatic. Doing this requires you to explicitly create a recordset object:

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQL, objConn, adOpenStatic, adLockReadOnly, adCmdText

Attempting to implicitly create a recordset, eg like this:

Set objRS = objConn.execute(strSQL)

will not work, as the implicitly created recordset will have a default adOpenForwardOnly cursor.

As mentioned above, the alternative method is to use a client-side cursor. The client referred in this case is the OLEDB Cursor Service.

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.CursorLocation = adUseClient
objRS.Open strSQL, objConn,,adLockReadOnly, adCmdText

In order to be able to use server-side cursors and .Recordcount, the Recordset object must support either Approximate Positioning or Bookmarking. There has been discussion on the ActiveServerPages list to the effect that the MS Oracle OLEDB Provider (or earlier versions of this provider) do not support either Approximate Positioning or Bookmarking, hence require client-side cursors in order for .RecordCount to work.

The use of ADO constants requires you to define them. You can get information on doing this here . For more information on the Recordset's .Open method click here .

Back to FAQ Listing

Так же в этом разделе:
 
MyTetra Share v.0.65
Яндекс индекс цитирования