【问题标题】:Microsoft Access find field in table with known valueMicrosoft Access 在具有已知值的表中查找字段
【发布时间】:2012-08-07 17:31:11
【问题描述】:

我们有一个相当大的 Oracle 数据库,我们可以通过 Microsoft Access 和 ODBC 以只读访问权限进行连接。我们使用的前端系统与幕后结构不匹配,我经常需要通过 Microsoft Access 查询系统。问题是我们没有提供任何关于结构的文档,结构需要认真注意。搜索我需要的字段非常耗时。

通过我们的前端,我可以查看我想要查询的值,并且我知道关键字段,但我需要找到包含已知值的字段。

如果我有一条我知道字段“A”的值的记录,并且有字段“X”的值,是否可以查询字段“X”?

前端展示

Student ID: 12345678
Payments: 23456

后端

TechID: 12345678
???: 23456

我可以查询“???”

【问题讨论】:

    标签: sql vba ms-access odbc


    【解决方案1】:

    您可以通过迭代表集合以及每个表的字段集合来实现此目的。

    Open Database
    Get all Tables
    For Each Table
       Get all Fields
       For Each Field
           If Field type is text ... and
           If Field size is not TOO Long ...
               Search for string
               If found, write to a results bucket
       Next
    Next
    

    这是一个编目表的代码示例(来源here

    Public Function GenerateDataDictionary(aDataDictionaryTable As String)
    '***       Usage: GenerateDataDictionary("MyDataDictionaryTable")
    '*** Extracts the information about the tables for the data dictionary 
    '*** and inserts it to a table named aDataDictionaryTable
    
        Dim tdf As TableDef, fldCur As Field, colTdf As TableDefs
        Dim rstDatadict As Recordset
        Dim i As Integer, j As Integer, k As Integer
        Set rstDatadict = CurrentDb.OpenRecordset(aDataDictionaryTable)
        Set colTdf = CurrentDb.TableDefs
    
        'Go through the database and get a tablename   
        For Each tdf In CurrentDb.TableDefs
        'Do what you want with the table names here.
        rstDatadict.AddNew
        rstDatadict.Update   
    rstDatadict.AddNew
        rstDatadict![Table] = tdf.NAME
        rstDatadict![Field] = "----------------------------"
        rstDatadict![Display] = "----------------------------"
        rstDatadict![Type] = ""
        rstDatadict.Update
        rstDatadict.AddNew
        rstDatadict![Table] = "Table Description:"
        For j = 0 To tdf.Properties.Count - 1
              If tdf.Properties(j).NAME = "Description" Then
                  rstDatadict![Field] = tdf.Properties(j).Value
              End If
        Next j
    
        rstDatadict.Update 
        rstDatadict.AddNew
        rstDatadict.Update
    
    For i = 0 To tdf.Fields.Count - 1
              Set fldCur = tdf.Fields(i)          
              rstDatadict.AddNew
              rstDatadict![Table] = tdf.NAME
              rstDatadict![Field] = fldCur.NAME
              rstDatadict![Size] = fldCur.Size
    
              Select Case fldCur.Type
                Case 1
                  FieldDataType = "Yes/No"
                Case 4
                  FieldDataType = "Number"
                Case 8
                  FieldDataType = "Date"
                Case 10
                  FieldDataType = "String"
                Case 11
                  FieldDataType = "OLE Object"
                Case 12
                  FieldDataType = "Memo"
                Case Else    ' Other values.
                  FieldDataType = fldCur.Type
              End Select
    
              rstDatadict![Type] = FieldDataType                                
                    For j = 0 To tdf.Fields(i).Properties.Count - 1
                        If fldCur.Properties(j).NAME = "Description" Then
                            rstDatadict![DESCRIPTION] = fldCur.Properties(j).Value
                        End If
    
                        If fldCur.Properties(j).NAME = "Caption" Then
                            rstDatadict![Display] = fldCur.Properties(j).Value
                        End If
    
                        If fldCur.Properties(j).NAME = "Rowsource" Then
                            rstDatadict![LookupSQL] = fldCur.Properties(j).Value
                        End If
                    Next j
    
                rstDatadict.Update
    
        Next i
        Debug.Print "  " & tdf.NAME
        Next tdf
    
    End Function
    

    您可以通过创建一个连接到表名表的字段名表来在 Access 中对您的发现进行分类。然后您的搜索将基于目录而不是原始集合。

    我以这种方式对 MAS 90(带有 JobOps 插件)的架构进行了逆向工程。没有地图,但我有一个只读的 ODBC 连接,我完全按照您建议的方式使用它。采购会计师会给我一个独特的产品编号,我会通过这个综合引擎运行它。随着时间的推移,我成功地将 700 个表从 18k 字段中提取到 20 个表和几百个字段。这使我们能够导出数据。

    【讨论】:

    • 我找到了新链接,它现在应该可以工作了。恭喜我们没有失明。
    • 谢谢,S。我打算稍后再玩。我喜欢dbAppendOnlyOpenRecordset。该选项似乎加快了行添加速度。再次感谢。
    【解决方案2】:

    您的问题的答案很简单。不,你不能那样做。

    我能想到两种解决方案。第一种是手动将所有值连接在一起,然后查找包含该值的行。这是不完美的,但可能有效:

    select *
    from (select t.*, ('|'""col1||'|'||col2+'|' . . .||'|') as allcols
          from t
         ) t
    where instr('|23456|', allcols) > 0
    

    这将在列中找到具有该值的任何行。可能足够接近你想要的。

    第二个是使用 UNPIVOT 做基本相同的事情。

    我强烈建议您花一点时间来查找字段之间的映射,然后在 Oracle 中创建一个视图,其中包含在应用程序中看到的字段名称。听起来这会在中期为您节省很多精力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多