如何通过对单元格值范围进行分组来对数据进行排序和提取?

时间:2019-06-21 23:02:49

标签: sql ms-access access-vba ms-access-2010

我正在尝试将MS Access用于数据库项目。

我基本上只想从如下提示中获取信息:

  

请应急编号:

信息将弹出与所有与该条目相关的所有特定信息。

以下示例:

Contingency
CONTINGENCY 'XX:1_XX'
DISCONNECT BUS 1
DISCONNECT BUS 2
DISCONNECT BUS 3
DISCONNECT BUS 4
DISCONNECT BUS 5
DISCONNECT BUS 6
DISCONNECT BUS 7
END

因此,如果我输入以下提示:Contingency 'XX:1-XX',则上面的所有信息都将显示在我的面前。这就是目标。我仅使用一列,因为它来自.txt文件。我认为我不需要其他列来进行标识。

我尝试使用Access,但是我不知道如何过滤数据以仅显示行输出而不是列输出。

我没有代码,我不精通SQL或其他任何语言。

结果:运行查询后仅弹出"CONTINGENCY 'XX:1_XX'"

1 个答案:

答案 0 :(得分:0)

也许某天这会有所帮助,是好是坏? :D

我建议将文本文件导入具有至少两个文本字段的称为[Contingencies]的访问表中:1)contingencyNumber 2)contingencyMessage,然后将click事件添加到表单设计中的按钮上,这将为基于msgbox的字符串构建在所输入的contingencyNumber的所有contingencyMessage字段上。

'define some variables
dim rstContingency as dao.recordset
dim txtInput, txtOutput, sql as string
txtInput = inputbox("Enter Contingency Number","Enter Contingency Number","NA")

'build a sql string
sql = "SELECT * FROM [Contingencies] WHERE contingencyNumber = '" & txtInput  & "'"

'assign recordset
set rstContingency = currentdb.openrecordset(sql, dbopensnapshot, dbreadonly, dbreadonly)

'loop through records until EOF (end of file)
'each time adding the message to the output string
while not rstContingency.eof
  txtOutput = txtOutput & vbcrlf
  rstContingency.movenext
wend 

'display original input and the accumulated messages for output
msgbox "Contingency: " & txtInput & vbcrlf & txtOutput