简介 本文将说明如何将查询或表的内容转换为用于许多在线论坛的BBCode。
我会承认自己受到了一些专业上的破坏,现在我将Access和VBA用于所有内容。 如果需要以任何方式来组织任何信息,那么我将首先创建一个数据库,然后逐步删除,直到使流程自动化为止。 昨天,我需要从数据库中获取一些信息,并将其发布到网络论坛。 但是我不能简单地复制粘贴它,因为我需要添加所有格式。 因此,我很快决定制定一个过程,该过程可以接受查询,表甚至SQL字符串,并将其转换为有效代码以在论坛中发布。
结果如下:
Public Function ConvertQueryToBB(strSource As String, Optional bIncludeHeaders As Boolean = True) As String
On Error GoTo errHandler
Dim rs As DAO.Recordset
Dim strTable As String
Dim iField As Integer
Dim strHeaderCellStart As String
Dim strHeaderCellEnd As String
strHeaderCellStart = "[td][size=12pt][b][color=blue]"
strHeaderCellEnd = "[/color][/b][/size][/td]"
Dim strCellStart As String
Dim strCellEnd As String
strCellStart = "[td]"
strCellEnd = "[/td]"
Set rs = CurrentDb.OpenRecordset(strSource, dbOpenDynaset, dbReadOnly)
'Start table
strTable = "[table]" & vbNewLine
'Optionally include row headers
If bIncludeHeaders Then
strTable = strTable & "[tr]"
For iField = 0 To rs.Fields.Count - 1
strTable = strTable & strHeaderCellStart & rs.Fields(iField).Name & strHeaderCellEnd
Next
strTable = strTable & "[/tr]"
End If
'Loop through recordset adding fields
Do While Not rs.EOF
strTable = strTable & "[tr]"
For iField = 0 To rs.Fields.Count - 1
strTable = strTable & strCellStart & rs.Fields(iField) & strCellEnd
Next
strTable = strTable & "[/tr]"
rs.MoveNext
Loop
'Close table
strTable = strTable & "[/table]"
'return string
ConvertQueryToBB = strTable
exitFunction:
If Not rs Is Nothing Then rs.Close
Set rs = Nothing
Exit Function
errHandler:
MsgBox Err.Number & " - " & Err.Description
Resume exitFunction
End Function
调用示例:
ConvertQueryToBB("TableName")
ConvertQueryToBB("TableName",False)
ConvertQueryToBB("QueryName")
ConvertQueryToBB("SELECT * FROM tbl_Users Where bAdmin=True ORDER BY FirstName")
论坛中的示例结果:
与往常一样,随时喜欢,评论,共享和使用发布的代码。
From: https://bytes.com/topic/access/insights/942630-convert-query-table-sql-web-forum-compatible-markup