如何拆分字符串并存储在 MS Access 表中 [英] How to Split a String and Store in MS Access table

查看:57
本文介绍了如何拆分字符串并存储在 MS Access 表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够拆分已输入到 MS Access 表单文本框中的文本字符串,并将拆分的字符串存储为同一字段下的单独记录.这是我到目前为止的代码,但我在每个角落都遇到问题.我对此相当陌生,但一直在快速学习.任何帮助表示赞赏.

I would like to be able to split a string of text that has been entered into a textbox on a MS Access form and store the split strings as separate records under the same field. This is the code I have so far, but I keep running into problems at every corner. I'm fairly new to this, but have been learning quickly. Any help is appreciated.

这是我想要完成的:如果我在文本框中输入以下文本(这是一个句子")并单击提交.我希望彼此将单词存储为公共字段下的单独记录.看似简单,却让人头疼不已.

Here is what I'd like to accomplish: If I enter the following text into a text box ("this is a sentence") and click submit. I would like each other the words to be stored as individual records under a common field. Seems simple, but it's causing quite a few headaches.

Private Sub Submit_Click()

    Dim SetDBConnection As ADODB.Connection
    Set SetDBConnection = CurrentProject.Connection

    Dim strInsertRecord As String

    Dim strNewPhrase As String
    Dim strStorePhrase As String

    strNewPhrase = textPhrase
    strStorePhrase = Split(NewPhrase)

    strInsertRecord = "INSERT INTO [FieldSplice] (words) VALUES (" & strStorePhrase & ")"
    SetDBConnection.Execute strInsertRecord

    textPhrase.Value = Null
End Sub

推荐答案

我有点不清楚你为什么有 ADODB 连接...这是连接到外部数据库吗?如果是这样,这是有道理的,但是您缺少一些代码以使插入正常工作.

I'm a little unclear on why you have the ADODB connection... is this connecting to an external database? If so, that makes sense, but then you are missing some code to get the insert to work properly.

如果这只是一个内部(本机)Access 表,那么我认为您不需要任何这些.这是一个简单的示例,说明如何将字符串拆分为单词(基于空格),然后将它们插入表中:

If this is just an internal (native) Access table, then I don't think you need any of that. Here is a simple example of how you would take a string, split it into words (based on a space) and then insert those into your table:

Dim textPhrase As String
Dim words() As String
Dim i As Integer

textPhrase = "This is a test"
words = Split(textPhrase, " ")

sql = "parameters P1 text; INSERT INTO [FieldSplice] (words) VALUES ([P1])"

Set query = CurrentDb.CreateQueryDef("FsInsert", sql)

For i = LBound(words) To UBound(words)
  query.Parameters("P1").Value = words(i)
  query.Execute
Next i

CurrentDb.QueryDefs.Delete ("FsInsert")

另一个有趣的注意事项——您不需要每次都声明插入.您可以设置参数,为参数赋值并多次执行插入命令.这包含在我上面的示例中.

One other note of interest -- you don't need to declare the insert each time. You can set a parameter, assign values to the parameter and execute the insert command multiple times. This is included in my example above.

您的代码试图说明:

insert into [table] (field) values ("value1", "value2", "value3")

这是你做不到的.除非您的数据库支持数组插入(例如 Oracle),否则这必须作为三个插入来完成.

Which you can't do. That has to be done as three inserts, unless your database supports array inserts (Oracle, for example).

多个字段只能这样工作:

The multiple fields only works like this:

insert into [table] (field1, field2) values ("value1", "value2")

其中每个值对应表中的一列.

Where each value corresponds to a column in the table.

这篇关于如何拆分字符串并存储在 MS Access 表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆