两个表中的固定宽度文本文件

时间:2014-05-21 21:11:14

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

我需要指出正确的方向。我需要从Access创建一个固定宽度的文件。数据将来自2个表(雇主和雇员)。文本文件必须显示雇主记录,然后下一个记录将包含雇员数据。然后使用生成的文件布局上传到单独的系统。

例如:

雇主表

RecordID - 2 char - always 01
EmployerNumber - 6 char
EmployerName - 25 char
ReportID (pk) - Not included on text file, only used to relate employer with employee

员工表

RecordID - 2 char - always 02
EmployeeNumber - 4 char
EmployeeName - 25 char
ReportID (fk) - Not included on text file, only used to relate employee with employer

生成的文字文件:

01012345PENGUINS
028686CROSBY
027777MALKIN
026666LEMIEUX
01012345ACME INC
021122BUGS BUNNY
021133DAFFY DUCK
029872ROAD RUNNER

2 个答案:

答案 0 :(得分:0)

对于初学者来说,你的问题有点复杂,但它有两个部分:

  1. 您需要加入2个表格。您应该谷歌搜索“SQL内部联接”以获取一些示例。
  2. 您需要以所需格式导出数据。您可以使用Left()功能执行此操作。
  3. 假设您的表名为Employee和Employer,请考虑以下SQL语句:

    Select
    ER.RecordID & 
    left(ER.EmployerNumber & "      ",6) & 
    left(ER.EmployerName & "                         ",25) & 
    EE.RecordID & 
    Left(EE.EmployeeNumber & "    ",4) & 
    Left(EE.EmployeeName & "                         ",25) as SingleCol
    From Employee EE
    Join Employer ER on EE.ReportID=ER.ReportID
    

    此查询连接2个表(请参阅底部的Join),首先显示Employer表中的3个字段,然后显示Employee表中的3个字段。带有所有空格的Left()语句用空格填充该字段。此查询将返回1个长文本字段。

答案 1 :(得分:0)

假设[ReportID]是整数,那么[Employer]的样本数据如下所示......

ReportID  EmployerNumber  EmployerName  RecordID
--------  --------------  ------------  --------
       1  012345          PENGUINS      01      
       2  012345          ACME INC      01      

......这对[员工] ......

ReportID  EmployeeNumber  EmployeeName  RecordID
--------  --------------  ------------  --------
       1  8686            CROSBY        02      
       1  7777            MALKIN        02      
       1  6666            LEMIEUX       02      
       2  1122            BUGS BUNNY    02      
       2  1133            DAFFY DUCK    02      
       2  9872            ROAD RUNNER   02      

查询

    SELECT 
        RecordID & EmployerNumber & EmployerName AS RowData,
        CDbl(ReportID) AS RowSort
    FROM Employer
UNION ALL
    SELECT 
        RecordID & EmployeeNumber & EmployeeName AS RowData,
        CDbl(ReportID) + Val(EmployeeNumber) / 10000 AS RowSort
    FROM Employee

返回

RowData            RowSort
-----------------  -------
01012345PENGUINS         1
01012345ACME INC         2
028686CROSBY        1.8686
027777MALKIN        1.7777
026666LEMIEUX       1.6666
021122BUGS BUNNY    2.1122
021133DAFFY DUCK    2.1133
029872ROAD RUNNER   2.9872

所以查询

SELECT RowData
FROM
    (
            SELECT 
                RecordID & EmployerNumber & EmployerName AS RowData,
                CDbl(ReportID) AS RowSort
            FROM Employer
        UNION ALL
            SELECT 
                RecordID & EmployeeNumber & EmployeeName AS RowData,
                CDbl(ReportID) + Val(EmployeeNumber) / 10000 AS RowSort
            FROM Employee
    )
ORDER BY RowSort

返回

RowData          
-----------------
01012345PENGUINS 
026666LEMIEUX    
027777MALKIN     
028686CROSBY     
01012345ACME INC 
021122BUGS BUNNY 
021133DAFFY DUCK 
029872ROAD RUNNER

只需在Access中保存最后一个查询,然后将其导出为文本。