VBA +字符串分割

发布于 2024-11-03 11:27:57 字数 1581 浏览 1 评论 0原文

我想要将 <= 1000 个字符的结果拆分为大小为 255 的数组,然后用它更新数据库中的字段。

VBA 中的字符串最多支持 255 个字符,那么我该如何做到这一点呢?

我使用MS-Access 2007作为我的数据库,要更新的字段类型是Memo,所以它可以存储超过255个字符,我已经检查过了。

这就是我的查询的样子

Coalsce(""SELECT (parentdesc & '/' & keyword) FROM All_Keywords_Mapping where item_id=" & rs1![item_id] & """, ""\;"")

Coalsce 是我编写的一个函数,我得到的结果如下:

“abc/123\;Bcd/123\;Bcs/sdasdas\;Casad/sdads\;Fea/dasd adsad\;Fea/Zero\;Lo/somer-tet\;"

现在我将此结果更新到另一个表中的字段和字段数据类型 Memo。当结果超过 255 个字符时,我收到更新查询错误,因此我进行了调试现在检查结果的长度是否< 255然后只更新,它工作得很好,但是当它超过时我无法做任何事情

我的合并函数

Function Coalsce(strSql As String, strDelim As String, ParamArray NameList() As Variant)
Dim db As Database
Dim rs As DAO.Recordset
Dim strList As String

    Set db = CurrentDb

    If strSql <> "" Then
        Set rs = db.OpenRecordset(strSql)

        Do While Not rs.EOF
            strList = strList & rs.Fields(0) & strDelim
            rs.MoveNext
        Loop

        strList = Mid(strList, Len(strDelim))
    Else

        strList = Join(NameList, strDelim)
    End If

    Coalsce = strList

End Function

我的查询。

strSql = Select DISTINCT " & rs1![item_id] & " as item_id, FindReplace(Coalsce(""SELECT (desc & '/' & kw) FROM AKM where item_id=" & rs1![item_id] & """, ""\;""), ""/"", ""\/"") as res
Set rs2 = db.OpenRecordset(strSql, dbOpenDynaset)
DoCmd.RunSQL ("Update data_Query set res=" & rs2.Fields("res").Value)

I want a split a result of <= 1000 characters into an array of size 255 and then update a field in the DB with that.

Strings in VBA support a max of 255 characters, so how do I go about doing this?

I am using MS-Access 2007 as my DB, The field type to be updates is Memo, so it can store more than 255 characters, I have checked this.

This is what my query looks like

Coalsce(""SELECT (parentdesc & '/' & keyword) FROM All_Keywords_Mapping where item_id=" & rs1![item_id] & """, ""\;"")

Coalsce is a function that I have written, and it I get a result like:

"abc/123\;Bcd/123\;Bcs/sdasdas\;Casad/sdads\;Fea/dasd
adsad\;Fea/Zero\;Lo/somer-tet\;"

now I am updating this result to a field in another table and the field datatype Memo. When the result exceeds 255 chars I get an update query error, so I debugged it and now checking if the length of the result < 255 then only update, it works perfectly fine, but when it exceeds I am unable to do anything.

My coalsce function

Function Coalsce(strSql As String, strDelim As String, ParamArray NameList() As Variant)
Dim db As Database
Dim rs As DAO.Recordset
Dim strList As String

    Set db = CurrentDb

    If strSql <> "" Then
        Set rs = db.OpenRecordset(strSql)

        Do While Not rs.EOF
            strList = strList & rs.Fields(0) & strDelim
            rs.MoveNext
        Loop

        strList = Mid(strList, Len(strDelim))
    Else

        strList = Join(NameList, strDelim)
    End If

    Coalsce = strList

End Function

My query

strSql = Select DISTINCT " & rs1![item_id] & " as item_id, FindReplace(Coalsce(""SELECT (desc & '/' & kw) FROM AKM where item_id=" & rs1![item_id] & """, ""\;""), ""/"", ""\/"") as res
Set rs2 = db.OpenRecordset(strSql, dbOpenDynaset)
DoCmd.RunSQL ("Update data_Query set res=" & rs2.Fields("res").Value)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2024-11-10 11:27:57

发生这种情况有两个潜在原因。最常见的是,您在带有 ORDER BY 的 SQL 语句中执行此操作,在这种情况下,备注字段会被截断为 255 个字符。但是,这似乎不太可能,因为您的函数返回的字符串长度超过 255 个字符,因此这不应该成为问题。事实上,当存在 ORDER BY 时,解决 255 个字符截断的方法之一是将备注字段包装在像 Left([MyMemoField], 4096) 这样的函数中。

另一个原因与备注字段的串联有关,在这种情况下 MemoField1 & MemoField2 将被截断为 255 个字符(并且通常以乱码结尾)。您没有提及源字段的数据类型,因此这似乎不太可能。

所以,我基本上怀疑你的 Coalsce() 函数。我的第一个猜测是您已经定义了一个返回类型:

  Public Function Coalsce()

...这将返回一个变体类型,因为您没有显式设置返回类型。因此,您应该将其更改为:

  Public Function Coalsce() As String

但是,您可能决定在所有字段均为 Null 时返回 Null,在这种情况下,您必须将返回类型声明为 Variant:

  Public Function Coalsce() As Variant

这看起来像对我来说不好的做法,因为变体处理起来很复杂。

也可能是 ByRef/ByVal 问题。在这种情况下,您确实希望通过 ByVal 处理事物,因为 ByRef 情况会导致连接备注字段出现问题(而 ByVal 避免了这种情况)。

话虽如此,我想知道为什么不能直接连接 SQL 语句中的字段,而是需要一个函数来完成它。

另外,您存储在备忘录字段中的返回数据让我产生了一些 WTF 反应——看起来您正在存储重复的数据,而且格式几乎无法使用。

所以,基本上,如果没有更多关于你在做什么、如何做以及为什么这样做的细节,任何人都不可能真正回答你的问题。

There are two potential reasons for this to happen. The most common is that you are doing this in a SQL statement with an ORDER BY, in which case memo fields get truncated to 255 characters. However, that doesn't seem likely, as your function is returning the string that's longer than 255 characters, so that shouldn't be an issue. Indeed, one of the ways to get around the 255-character truncation when there's an ORDER BY is to wrap the memo field in a function like Left([MyMemoField], 4096).

The other cause is related to concatenation of memo fields, in which case MemoField1 & MemoField2 will be truncated at 255 characters (and often end in gibberish). You don't say anything about the data types of the source fields, so this seems unlikely.

So, I'm basically suspecting your Coalsce() function. My first guess is that you have defined a return type:

  Public Function Coalsce()

...that will return a variant type because you haven't explicitly set the return type. So, you should change it to this:

  Public Function Coalsce() As String

However, it could be that you've decided you want to return Null when all the fields are Null, in which case, you'd have to declare the return type as Variant:

  Public Function Coalsce() As Variant

This seems like bad practice to me, as Variants are complicated to deal with.

It could also be a ByRef/ByVal problem. You really want things to be processed ByVal in this kind of case, as it's the ByRef situation that leads to the problem concatenating memo fields (and ByVal avoids it).

All that said, I wonder why in the world you can't just concatenate the fields in your SQL statement, instead of needing a function to do it.

Also, the data returned that you're storing in the memo field prompts something of a WTF reaction from me -- it looks like you're storing duplicate data, and in a format that's almost impossible to use.

So, basically, without more details on what you're doing, how you're doing it and why you're doing it, it's impossible for anybody to really answer your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文