-Поиск по дневнику

Поиск сообщений в rss_sql_ru_access_programming

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 16.03.2006
Записей:
Комментариев:
Написано: 4


Ошибка object reqired при очистке БД

Суббота, 22 Апреля 2017 г. 10:17 + в цитатник
Доброго времени суток. Пытаюсь сделать программу для импорта данных из таблицы Word в Access.

Нашёл на иностранном сайте код:
Private Sub cmdImport_Click()
Dim appWord As Word.Application, doc As Word.Document
Dim dbs As DAO.Database, rst As DAO.Recordset, strDoc As String
 
    Set appWord = CreateObject("Word.Application") 'establish an instance of word
    strDoc = CurrentProject.Path & "\People.docx"  'set string to document path and file
    Set doc = appWord.Documents.Open(strDoc) 'establish the document
 
    Set dbs = CurrentDb 'establish the database to use (this is our current Database)
    Set rst = dbs.OpenRecordset("tblPeople") 'establish the recordset
 
    With doc.Tables(1) 'target table 1 in People.docx
 
        For i = 2 To .Rows.Count 'cycle through rows in Tables(1) [we skip the first row because the table has headers]
 
            With rst
                .AddNew 'creating a new record
                    ![FName] = doc.Tables(1).Cell(i, 1).Range.Text
                    ![LName] = doc.Tables(1).Cell(i, 2).Range.Text
                    ![SSN] = doc.Tables(1).Cell(i, 3).Range.Text
                    ![PhoneNumber] = doc.Tables(1).Cell(i, 5).Range.Text
                    ![Gender] = doc.Tables(1).Cell(i, 6).Range.Text
                .Update 'update the whole record
            End With
 
        Next 'go to next row in Tables(1)
 
    End With
 
    rst.Close: Set rst = Nothing 'close and clear recordset
    db.Close: Set rst = Nothing 'close and clear database
    doc.Close: Set doc = Nothing 'close and clear document
    appWord.Quit: Set appWord = Nothing 'close and clear MS Word
 
End Sub


Создал в ворде тестовую табличку, создал таблицу в Access для заполнения, перебил код под свой вордовский файл, привёл к такому виду
Private Sub Кнопка0_Click()
Dim appWord As Word.Application, doc As Word.Document
Dim dbs As DAO.Database, rst As DAO.Recordset, strDoc As String
     
        Set appWord = CreateObject("Word.Application") 'establish an instance of word
        strDoc = "C:\Documents and Settings\Admin\Рабочий стол\tblTest.docx"  'set string to document path and file
        Set doc = appWord.Documents.Open(strDoc) 'establish the document
     
        Set dbs = CurrentDb 'establish the database to use (this is our current Database)
        Set rst = dbs.OpenRecordset("tblTest") 'establish the recordset
     
        With doc.Tables(1) 'target table 1 in People.docx
     
            For i = 2 To .Rows.Count 'cycle through rows in Tables(1) [we skip the first row because the table has headers]
     
                With rst
                    .AddNew 'creating a new record
                        ![ob] = doc.Tables(1).Cell(i, 1).Range.Text
                        ![smr] = doc.Tables(1).Cell(i, 2).Range.Text
                        ![emm] = doc.Tables(1).Cell(i, 3).Range.Text
                        ![mat] = doc.Tables(1).Cell(i, 4).Range.Text
                    .Update 'update the whole record
                End With
     
            Next 'go to next row in Tables(1)
     
        End With
     
        rst.Close: Set rst = Nothing 'close and clear recordset
        db.Close: Set rst = Nothing 'close and clear database
        doc.Close: Set doc = Nothing 'close and clear document
        appWord.Quit: Set appWord = Nothing 'close and clear MS Word
     
    End Sub


(Строку strDoc = CurrentProject.Path был вынужден заменить на путь, т.к. в таком виде файл не устанавливался)
Пришёл к тому, что при запуске выдаётся object reqired и подчёркивается строка очистки базы данных
db.Close: Set rst = Nothing 'close and clear database


Есть также англоязычное видео, где используют этот вот код. Ничего не меняют и всё работает. Не могу понять в чём я провинился

http://www.sql.ru/forum/1258017/oshibka-object-reqired-pri-ochistke-bd


 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку