-2

I am building a library database and i have a working script to probe a web database using the ISBN number and return data about the book. I have successfully made the data print to the immediate window using debug.print and then the specific property of the object. I am now wanting to print the data retrieved straight in to the database.

here is my code for the ISBN search:

Option Compare Database
    Dim BookTitle As String
    Dim BookTitleLong As String
    Dim BookAuthorsText As String
    Dim BookPublisherText As String
    Dim BookSummary As String
    Dim BookNotes As String
    Dim accessKey As String

Private Sub Class_Initialize()
    'Your isbnDB access key'
    accessKey = "xxxxxx" 'Working access key here
End Sub
Property Get Title() As String
    Title = BookTitle
End Property
Property Get TitleLong() As String
    TitleLong = BookTitleLong
End Property
Property Get AuthorsText() As String
    AuthorsText = BookAuthorsText
End Property
Property Get PublisherText() As String
    PublisherText = BookPublisherText
End Property
Property Get Summary() As String
    Summary = BookSummary
End Property
Property Get Notes() As String
    Notes = BookNotes
End Property

Public Function Lookup(ISBN As String) As Boolean
    Lookup = False
    Dim xmlhttp
    Set xmlhttp = CreateObject("MSXML2.xmlhttp")
    xmlhttp.Open "GET", "https://isbndb.com/api/books.xml?access_key=" & accessKey & "&results=texts&index1=isbn&value1=" & ISBN, False
    xmlhttp.send
    'Debug.Print "Response: " & xmlhttp.responseXML.XML'
    Dim xmldoc
    Set xmldoc = CreateObject("Microsoft.XMLDOM")
    xmldoc.async = False
    'Note: the ResponseXml property parses the server's response, responsetext doesn't
    xmldoc.loadXML (xmlhttp.responseXML.XML)
    If (xmldoc.selectSingleNode("//BookList").getAttribute("total_results") = 0) Then
        MsgBox "Invalid ISBN or not in database"
        Exit Function
    End If
    If (xmldoc.selectSingleNode("//BookList").getAttribute("total_results") > 1) Then
        MsgBox "Caution, got more than one result!"
        Exit Function
    End If
    BookTitle = xmldoc.selectSingleNode("//BookData/Title").Text
    BookTitleLong = xmldoc.selectSingleNode("//BookData/TitleLong").Text
    BookAuthorsText = xmldoc.selectSingleNode("//BookData/AuthorsText").Text
    BookPublisherText = xmldoc.selectSingleNode("//BookData/PublisherText").Text
    BookNotes = xmldoc.selectSingleNode("//BookData/Notes").Text
    BookSummary = xmldoc.selectSingleNode("//BookData/Summary").Text
    Lookup = True
End Function

and here is the code i have used to print to the immediate window

Public Function t()
    Dim book
    Set book = New ISBN
    book.Lookup ("0007102968")
    Debug.Print book.Title
    Debug.Print book.PublisherText
    Debug.Print book.AuthorsText
    Debug.Print book.TitleLong
    Debug.Print book.Summary
    Debug.Print book.Notes

End Function

this is all based off this question asked a few years back: ISBN -> bookdata Lookup to fill in a database

i would also like to be able to input the ISBN through a form if anyone can help with that :)

Erik A
  • 31,639
  • 12
  • 42
  • 67
Sam Brown
  • 1
  • 2
  • 2
    So... you want to know how to write an `INSERT` statement? Or is there any reason that won't do? Really, this is basic stuff, and you should read in how to store things in a database yourself, and make a good-faith attempt. Then you can ask questions if things don't work as expected. – Erik A Aug 17 '17 at 09:55
  • hi thanks for the pointer, I'm completely new to this. i have tried running this: `StrSQL = "INSERT INTO Table1 (Title) VALUES (book.Title)"` but instead of inserting the object property `book.Title` it brings up a text box for me to fill it in manually – Sam Brown Aug 17 '17 at 10:44
  • 1
    Well, we're not an "I can't code so please code for me for free" site. We are willing to help, but only if you put in the work and make a good-faith attempt yourself. There are numerous resources on how to execute queries. Read about them, and specifically calling queries from VBA (because that's where your variables reside), and using parameters. – Erik A Aug 17 '17 at 10:49
  • Don't enclose variable references within quotes and concatenate instead. Text field parameters need apostrophe delimiters. Try:`StrSQL = "INSERT INTO Table1 (Title) VALUES ('" & book.Title & "')"` – June7 Aug 17 '17 at 11:14
  • @ErikvonAsmuth i appreciate what you are saying but i have been trying to fix this alone for the last day and a half and i haven't asked you to write any code for me i simply asked for some pointers on where to go next. – Sam Brown Aug 17 '17 at 12:26
  • @June7 thanks for the help, i see what you mean about not calling the variables in the quotes but i have not seen any success with your revision to my SQL commant – Sam Brown Aug 17 '17 at 12:26
  • @SamBrown The main problem is that you're not sharing your attempts, and why they haven't worked, in your question, nor are you sharing any table structure. A simple google on `Access VBA Insert Table` will result in [this question](https://stackoverflow.com/questions/25044447/how-to-insert-values-into-the-database-table-using-vba-in-ms-access), which looks perfectly valid for your use case. – Erik A Aug 17 '17 at 12:44
  • Furthermore, your original question contained your private isbnDB access key. Please change that key, since it's private (not to be shared publicly on the internet). – Erik A Aug 17 '17 at 12:45
  • @ErikvonAsmuth i found the article you have linked following the reference to you made to `INSERT` statements but am still struggling. thank you for the help – Sam Brown Aug 17 '17 at 14:38
  • What does 'not seen any success' mean, what happens - error message, wrong results, nothing? You haven't shown how you use the constructed SQL string. Edit your question with this information. Are you able to perform the INSERT with an Access query object using static parameter? – June7 Aug 17 '17 at 17:15

1 Answers1

1

You can try the following approach.

First of all, create a user-defined data type to store the book data:

Public Type Book
    ISBN As String
    Title As String
    TitleLong As String
    AuthorsText As String
    PublisherText As String
    Summary As String
    Notes As String
End Type

Then create an insert query and pass the book values as parameters. Let's name the query qryAdd.

PARAMETERS prmISBN Text (255), 
           prmTitle Text (255), 
           prmPublisherText Text (255), 
           prmAuthorsText Text (255), 
           prmTitleLong Text (255), 
           prmSummary LongText, 
           prmNotes LongText;

INSERT INTO T ( ISBN, Title, PublisherText, AuthorsText, TitleLong, Summary, Notes )
SELECT prmISBN AS ISBN, 
       prmTitle AS Title, 
       prmPublisherText AS PublisherText, 
       prmAuthorsText AS AuthorsText, 
       prmTitleLong AS TitleLong, 
       prmSummary AS Summary, 
       prmNotes AS Notes;

'Change T to the name of your table and update the field names.

Lastly, the function to call the insert query where we pass the book to be inserted.

Public Function InsertToDatabase(b As Book) As Boolean
    With CurrentDb().QueryDefs("qryAdd")
        .Parameters("[prmISBN]").Value = b.ISBN
        .Parameters("[prmTitle]").Value = b.Title
        .Parameters("[prmTitleLong]").Value = b.TitleLong
        .Parameters("[prmPublisherText]").Value = b.PublisherText
        .Parameters("[prmAuthorsText]").Value = b.AuthorsText
        .Parameters("[prmSummary]").Value = b.Summary
        .Parameters("[prmNotes]").Value = b.Notes
        .Execute dbFailOnError
    End With

    'all good
    InsertToDatabase = True
End Function

To test it:

Sub Test()
    Dim b As Book
        b.ISBN = "aaa"
        b.Title = "bbb"
        b.TitleLong = "ccc"
        b.PublisherText = "ddd"
        b.AuthorsText = "eee"
        b.Summary = "fff"
        b.Notes = "ggg"

    If InsertToDatabase(b) Then MsgBox "Done!"
End Sub
Kostas K.
  • 8,293
  • 2
  • 22
  • 28