ISBN -> bookdata Lookup to fill in a database

后端 未结 5 1915
情歌与酒
情歌与酒 2021-01-03 13:37

Ok, I wanting to database a small library.

I\'ve only had limited experience with databases, and none with querying from a webserver.

I\'m going to want to r

5条回答
  •  长发绾君心
    2021-01-03 13:54

    I recently had to do exactly this as we indexed our library for insurance purposes. Below is the code of the vba class I hacked together:

    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 = "PUT ACCESSKEY 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
    

    Get an API key, paste the above code (with your key) into a new class module in the VBA editor (Insert->Class Module) and name the module "isbn". You also need to add a reference to "Microsoft XML" in the VBA editor (Tools->References)

    You can test it works with the code snippet below in a normal vba module:

    Public Function testlookup()
        Dim book
        Set book = New isbn
        book.Lookup ("0007102968")
        Debug.Print book.Title
        Debug.Print book.PublisherText
    End Function
    

    Then just type "testlookup" into the immediate window (View->Immediate Window). You should see a response of:

    The Times book of quotations
    [Glasgow] : Times Books : 2000.
    

    isbnDB can return more than the data I collect in the above class, read the API reference here: http://isbndb.com/docs/api/ and tailor the class to your needs.

    I found this article very helpful in explaining how to use xmlhttp from within access: http://www.15seconds.com/issue/991125.htm

    The msdn pages on XML DOM Methods and XMLHttpRequest Object were also useful (because I'm a new user I can only post two active links, you'll have to replace the dots in the urls below):

    msdn microsoft com/en-us/library/ms757828(v=VS.85).aspx

    msdn microsoft com/en-us/library/ms535874(v=vs.85).aspx

提交回复
热议问题