【问题标题】:Using VBA Function in Access Query在访问查询中使用 VBA 函数
【发布时间】:2021-03-23 11:59:23
【问题描述】:

我想使用 MS access 编写一个 SQL 查询,它使用 VBA 函数计算序列号。

我有一个很大的“samplebasicinformation”表,其中有很多外键,我想使用外表中的文本字段来创建基于文本的序列号。

我希望查询的第一个字段缩写其他表中的几个文本字段,然后将它们连接起来并创建此序列号。

为了缩写文本字段,我有以下功能:

Function GetFirstLetters(rng As String)
Dim arr
Dim I As Long
arr = VBA.Split(rng, " ")
If IsArray(arr) Then
    For I = LBound(arr) To UBound(arr)
        GetFirstLetters = GetFirstLetters & Left(arr(I), 1)
    Next I
Else
    GetFirstLetters = Left(arr, 1)
End If
End Function

我已尝试使用以下 SQL 来实现此目的,但由于语法错误而失败。

SELECT 
(getfirstletters(select sl.locationname from samplebasicinformation as sbi join samplelocation as sl 
on sbi.samplelocationid = sl.samplelocationid)), 
samplebasicinformationid 
from samplebasicinformation as sbi1

谁能给点建议?

【问题讨论】:

    标签: sql vba ms-access


    【解决方案1】:

    您需要为您的函数提供一列。

    您的查询比较混乱,我认为就这么简单:

    SELECT 
      getfirstletters(sl.locationname),
      sbi.samplebasicinformationid 
    FROM samplebasicinformation as sbi 
      INNER JOIN samplelocation as sl 
      on sbi.samplelocationid = sl.samplelocationid
    

    注意,Access Sql 中需要INNER JOIN

    【讨论】:

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