微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

access转json

Access转换为Json删除访问操作的复杂性,让您为您的应用程序提供更好的数据可用性。

access转json

在执行此操作之前,请确保使用的是Microsoft Access 2010或更高版本。 要将Access数据库转换为JSON格式,您需要安装Access转换器。

  Option Compare Database
  Option Explicit

  ' Convert to JSON button click event
  Private Sub cmdConvertToJson_Click()

    Dim strjsonOutput As String
    
    ' Use our function to handle conversion
    strjsonOutput = ConvertToJson()
    
    ' Output to the text Box control
    Me.txtJsonOutput.Value = strjsonOutput
  
  End Sub

  ' Function to convert the current Access table to JSON format
  Public Function ConvertToJson() As String

    Dim strsql As String
    Dim strjsonOutput As String
    Dim objTable As DAO.Recordset
    Dim objField As DAO.Field
    
    ' Get the current table name from the comboBox control
    strsql = "SELECT * FROM " & Me.cboTableNames.Value
    
    ' Open a Recordset object for the current table
    Set objTable = CurrentDb.OpenRecordset(strsql,dbOpenSnapshot)
    
    ' Start building our JSON string
    strjsonOutput = "{""data"": ["
    
    Do Until objTable.EOF
  
      ' Start a new JSON object
      strjsonOutput = strjsonOutput & "{"
      
      ' Loop through each field in the recordset and add it to the JSON object
      For Each objField In objTable.Fields
        If objField.Type  dbAttachment And Not IsNull(objField.Value) Then
          strjsonOutput = strjsonOutput & """" & objField.Name & """:""" & objField.Value & ""","
        End If
      Next objField
      
      ' Remove any trailing comma from the JSON object
      If Right(strjsonOutput,1) = "," Then
        strjsonOutput = Left(strjsonOutput,Len(strjsonOutput) - 1)
      End If
      
      ' End the current JSON object
      strjsonOutput = strjsonOutput & "},"
      
      ' Move to the next record
      objTable.MoveNext
    
    Loop
    
    ' Remove any trailing comma from the JSON string
    If Right(strjsonOutput," Then
      strjsonOutput = Left(strjsonOutput,Len(strjsonOutput) - 1)
    End If
    
    ' End the JSON string
    strjsonOutput = strjsonOutput & "]}"
    
    ' Close the recordset and return the JSON string
    objTable.Close
    Set objTable = nothing
    ConvertToJson = strjsonOutput
  
  End Function

以上代码将选定的表转换为Json格式并存储到输出文本框中。

现在您可以使用此功能在Access中将数据输出到Json格式,从而使数据可用于您的应用程序。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐