从字段按分号或句号分段提取字符串,并存储到其他表中的实例-pzszdy
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


从字段按分号或句号分段提取字符串,并存储到其他表中的实例

发表时间:2010/9/11 12:47:59 评论(0) 浏览(10217)  评论 | 加入收藏 | 复制
   
摘 要:字符 分段 提取
正 文:

有时候一个字段里面存储了几句话,我们想按分号或者句号,分段提取,并存储到另外的表中,可以用这样的方法,我是初学,也许还有更好的方法,请大家指教。

代码是我的一个实例,并不通用:

Option Compare Database

Private Sub Command0_Click()
Dim strCf(1 To 10) As String '分段处罚
Dim strsqlCfjg As String '查询到的处罚结果
Dim intWzfh '分号所在位置
Dim intWzjh '句号所在位置
Dim intWzks '开始位置
Dim intCsfh As Integer '分号开始位置

 

       Dim ssql As String
        ssql = "Delete * FROM 处罚分段;"
        CurrentDb.Execute ssql
        ssql = "Insert INTO 处罚分段 ( 案件编号)   VALUES('" & [Forms]![窗体1].[List1] & "') "
        CurrentDb.Execute ssql

     Dim sql As String
     Dim rs As ADODB.Recordset
   
   
     sql = "Select 案件基本情况.处罚结果 FROM 案件基本情况  Where 案件基本情况.案件编号='" & [Forms]![窗体1].[List1] & "'"
     Set rs = New ADODB.Recordset
     rs.CursorLocation = adUseClient
     rs.Open sql, CurrentProject.Connection, adopendynaset, adLockOptimistic
    
    
    
If rs!处罚结果 <> "" Then
       strsqlCfyj = rs!处罚结果
     
    ' strsqlCfjg = "Select 案件基本情况.处罚结果 FROM 案件基本情况 Where 案件基本情况.案件编号='" & [Forms]![工作台].[List116] & "'"
    ' CurrentDb.Execute trsqlCfjg
      strsqlCfyj = Replace(Replace(strsqlCfyj, Chr(10), ""), Chr(13), "")
      strsqlCfyj = Replace(strsqlCfyj, ";", ";")
      strsqlCfyj = Replace(strsqlCfyj, ".", "。")
      strsqlCfyj = Replace(strsqlCfyj, ",", ",")
  
    k = Len(strsqlCfyj)
    intCsfh = 0
    For l = 1 To k
        If Mid(strsqlCfyj, l, 1) = ";" or Mid(strsqlCfyj, l, 1) = "。" Then
        intCsfh = intCsfh + 1
        End If
    Next
 
     intWzfh = 1
     intWzks = 1
    
     For I = 1 To intCsfh '分、句号次数
       intWzfh = IIf(InStr(intWzfh, strsqlCfyj, ";") = 0, InStr(intWzfh, strsqlCfyj, "。"), InStr(intWzfh, strsqlCfyj, ";"))
      ' intWzjh = InStr(intWzfh, strsqlCfyj, "。")
      
         For J = intWzks To intWzfh
           strCf(I) = strCf(I) + Mid(strsqlCfyj, J, 1)
         Next
       intWzfh = intWzfh + 1
       intWzks = intWzfh
       strCf(I) = Trim(strCf(I))
        'ssql = "Insert INTO 处罚分段 ( 处罚1)   VALUES('" & strCf(I) & "') "
        'CurrentDb.Execute ssql
        ssql = "Update 处罚分段 SET 处罚分段.处罚" & I & " = '" & strCf(I) & "'  Where  处罚分段.案件编号 = '" & [Forms]![窗体1].[List1] & "'"
        CurrentDb.Execute ssql
      MsgBox strCf(I)
    Next

 

End If
End Sub



 


Access软件网交流QQ群(群号:198465573)
 
 相关文章
【Access源码示例】Access正则匹配指定字符串长度  【漏蛧尐魚℡  2013/4/20】
零长度字符串与空值的区别  【殷小宝  2013/4/20】
【Access小品】替换批量Word文件中的字符串示例  【煮江品茶  2013/4/27】
InstrRev函数,从右边算起,获取字符串在另一个字符串中出现的...  【宏鹏  2013/5/1】
access中null和空字符串的区别  【小赵  2013/5/6】
常见问答
技术分类
相关资源
文章搜索
关于作者

pzszdy

文章分类

文章存档

友情链接