Office中國(guó)論壇/Access中國(guó)論壇

 找回密碼
 注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

返回列表 發(fā)新帖
樓主: tmtony
打印 上一主題 下一主題

[Access本身] Access技巧接龍

[復(fù)制鏈接]
21#
發(fā)表于 2004-12-17 20:07:00 | 只看該作者
讓表中的所有空值等于其對(duì)應(yīng)字段的上一條記錄值的函數(shù),rstName是表名或查詢的名稱,要引用DAOPublic Function InsNullFld(rstName As String) As Boolean

    Dim rst1 As DAO.Recordset, rst2 As DAO.Recordset

    Dim i As Integer, isChn As Boolean

    Set rst1 = CurrentDb().OpenRecordset(rstName)

    Set rst2 = CurrentDb().OpenRecordset(rstName)

    rst2.MoveNext

    Do While Not rst2.EOF

        rst2.Edit

        isChn = False

        For i = 0 To rst2.Fields.Count - 1

            If IsNull(rst2.Fields(i)) Then

                rst2.Fields(i) = rst1.Fields(i)

                isChn = True

                InsNullFld = True

            End If

        Next

        If isChn Then rst2.Update

        rst1.MoveNext

        rst2.MoveNext

    Loop

End Function

22#
發(fā)表于 2004-12-17 21:25:00 | 只看該作者
如果一對(duì)多關(guān)系的表,用了“自動(dòng)編號(hào)”連接,就會(huì)備份后不能還原,因?yàn)椤白詣?dòng)編號(hào)”不能寫入。

可以用辦法代替“自動(dòng)編號(hào)”,也能實(shí)現(xiàn)“自動(dòng)編號(hào)”的功能。下面辦法是“Lwwvb”版主發(fā)明的,我把它改進(jìn)了。

支持多用戶同時(shí)使用,不會(huì)沖突,不會(huì)重復(fù)編號(hào)。可以完全代替“自動(dòng)編號(hào)”

1;做一個(gè)表,只有一行,放個(gè) Int 類型的字段“ID”,初值為“0”用來(lái)放參考編號(hào),

2;做一個(gè)“更新查詢”更新這個(gè)“ID”,使每次執(zhí)行“[ID] + 1”作為“自動(dòng)編號(hào)”用。

3;在窗體的“插入前”寫事件,先執(zhí)行“更新查詢”,然后取得這個(gè)“ID”值放在窗體“ID”控件的“默認(rèn)值”里面。再在“插入后”事件清除“默認(rèn)值”。



注意:要有控件綁定“ID”,控件才有“默認(rèn)值”,窗體的字段沒(méi)有“默認(rèn)值”,數(shù)據(jù)表可以使用隱藏列。

[此貼子已經(jīng)被作者于2004-12-17 13:26:14編輯過(guò)]

23#
發(fā)表于 2004-12-19 01:01:00 | 只看該作者
可是很不幸,上面那種方法不能在 ODBC 中的 數(shù)據(jù)表 粘貼多行。
24#
發(fā)表于 2004-12-20 17:52:00 | 只看該作者
由字段名稱求字段標(biāo)題函數(shù)及由字段標(biāo)題求字段名稱函數(shù)ublic Function GetFldName(TblName As String, FldCaption As String) As String

On Error Resume Next

    Dim rst As DAO.Recordset, i As Integer

    Set rst = CurrentDb().OpenRecordset(TblName)

        For i = 0 To rst.Fields.Count - 1

            If rst.Fields(i).Properties("Caption") = FldCaption Then

                GetFldName = rst.Fields(i).Name

                Exit For

            End If

        Next

End FunctionPublic Function GetFldCaption(TblName As String, FldName As String) As String

On Error Resume Next

    Dim rst As DAO.Recordset

    Set rst = CurrentDb().OpenRecordset(TblName)

    GetFldCaption = rst.Fields(FldName).Properties("Caption")

End Function

如果在窗體內(nèi)部應(yīng)用,則不需要表名參數(shù),用Me.Recordset來(lái)代替上面函數(shù)的Rst即可。Private Function GetFldName(FldCaption As String) As String

On Error Resume Next

       Dim i As Integer

       For i = 0 To Me.Recordset.Fields.Count - 1

            If Me.Recordset.Fields(i).Properties("Caption") = FldCaption Then

                GetFldName = Me.Recordset.Fields(i).Name

                Exit For

            End If

        Next

End FunctionPrivate Function GetFldCaption(FldName As String) As String

       GetFldCaption = Me.Recordset.Fields(FldName).Properties("Caption")

End Function

25#
發(fā)表于 2004-12-28 19:13:00 | 只看該作者
關(guān)于取得漢字的第一個(gè)拼音字母的函數(shù),推薦使用我的GetPyAbOfHz函數(shù).速度比上述HZPY快一倍. 不信? 測(cè)試代碼附后Public Function GetFirstChr(ByVal sSrc As String)

    Dim sTemp As String

    sTemp = Left(sSrc, 1)

Select Case Asc(sTemp)

    Case 48 To 122

         GetFirstChr = sTemp

    Case Is >= Asc("匝")

         GetFirstChr = "Z"

    Case Is >= Asc("壓")

         GetFirstChr = "Y"

    Case Is >= Asc("昔")

         GetFirstChr = "X"

    Case Is >= Asc("挖")

         GetFirstChr = "W"

    Case Is >= Asc("塌")

         GetFirstChr = "T"

    Case Is >= Asc("撒")

         GetFirstChr = "S"

    Case Is >= Asc("然")

         GetFirstChr = "R"

    Case Is >= Asc("期")

         GetFirstChr = "Q"

    Case Is >= Asc("啪")

         GetFirstChr = ""

    Case Is >= Asc("哦")

         GetFirstChr = "O"

    Case Is >= Asc("拿")

         GetFirstChr = "N"

    Case Is >= Asc("媽")

         GetFirstChr = "M"

    Case Is >= Asc("垃")

         GetFirstChr = "L"

    Case Is >= Asc("喀")

         GetFirstChr = "K"

    Case Is >= Asc("擊")

         GetFirstChr = "J"

    Case Is >= Asc("哈")

         GetFirstChr = "H"

    Case Is >= Asc("噶")

         GetFirstChr = "G"

    Case Is >= Asc("發(fā)")

         GetFirstChr = "F"

    Case Is >= Asc("蛾")

         GetFirstChr = "E"

    Case Is >= Asc("搭")

         GetFirstChr = "D"

    Case Is >= Asc("擦")

         GetFirstChr = "C"

    Case Is >= Asc("芭")

         GetFirstChr = "B"

    Case Is >= Asc("啊")

         GetFirstChr = "A"

    Case Else

         GetFirstChr = "0"

End Select

End Function

'取得漢字的拼音縮寫

Public Function GetPyAbOfHz(ByVal sHz As String) As String

    Dim sTmp, ch As String

    Dim i As Integer

    For i = 1 To Len(sHz)

        ch = Mid(sHz, i, 1)

        Select Case ch

            Case ".", "(", ")", "+", "-"

                sTmp = sTmp & ch

            Case " "

            Case Else

                sTmp = sTmp & GetFirstChr(ch)

        End Select

    Next

    GetPyAbOfHz = sTmp

End Function

'----------------------------------------速度測(cè)試代碼:---------Public Sub speed_HZPY()

    Dim a1 As Single

        Dim i As Long    a1 = Timer

    For i = 1 To 66542

        HZPY ("地要不是在遙吶喊濁奪標(biāo)熾烈二有遙感地于規(guī)劃")

    Next

    Debug.Print Timer - a1

End Sub

Public Sub SPEED_GetPyAbOfHz()

    Dim a1 As Single

    Dim i As Long

    a1 = Timer

    For i = 1 To 66542

        GetPyAbOfHz ("地要不是在遙吶喊濁奪標(biāo)熾烈二有遙感地于規(guī)劃")

    Next

    Debug.Print Timer - a1

   

End Sub
26#
發(fā)表于 2004-12-30 01:20:00 | 只看該作者
漢字拼音首字母,我也貼一個(gè),不過(guò)不是我寫的。呵呵!'HZPY(String):返回漢字拼音首字母函數(shù):Function HZPY(hzstr As String) As String

Dim p0 As String, C As String, STR As String

Dim i As Integer, j As Integer

p0 = "吖八嚓咑妸發(fā)旮鉿譏譏咔垃呣拿謳趴七呥仨他哇哇哇夕丫匝咗"

For i = 1 To Len(hzstr)

    C = "z"

    STR = Mid(hzstr, i, 1)

    If Asc(STR) > 0 Then

        C = STR

    Else

        For j = 1 To 26

            If Mid(p0, j, 1) > STR Then

                C = Chr(95 + j)

                Exit For

            End If

        Next

    End If

    HZPY = HZPY + C

Next

End Function
27#
發(fā)表于 2004-12-30 21:56:00 | 只看該作者
如何使窗體只能新增記錄,不能修改舊記錄在窗體的成為當(dāng)前記錄時(shí)間增加如下語(yǔ)句Private Sub Form_Current()

    Me.AllowEdits = Me.NewRecord

End Sub

28#
發(fā)表于 2005-1-10 04:22:00 | 只看該作者

回樓上

回樓上,試過(guò)好用

但:如果strNum 尾數(shù)為0時(shí)會(huì)出錯(cuò),如strNum =10,算法還得再加一句.
29#
發(fā)表于 2005-1-20 19:51:00 | 只看該作者
以下是引用靈芝在2005-1-9 20:22:29的發(fā)言:



回樓上,試過(guò)好用

但:如果strNum 尾數(shù)為0時(shí)會(huì)出錯(cuò),如strNum =10,算法還得再加一句.

Function AutoNum(strNum As String) As String

    AutoNum = StrReverse(Val(StrReverse(strNum)))

    AutoNum = IIf(Right(strNum, 1) = "0", Left(strNum, Len(strNum) - 1) & "1", Left(strNum, Len(strNum) - Len(AutoNum)) & Format((AutoNum + 1), String(Len(AutoNum), "0")))

End Function

30#
發(fā)表于 2005-1-20 19:57:00 | 只看該作者
支持拼音首字母的組合框函數(shù)及實(shí)例(非常實(shí)用,能大大提高選擇和錄入速度)

Public Function ComboBoxKeyPress(KeyAscii As Integer) As String

'調(diào)用方法  在組合框的keypress方法中加入:

'--------------------------------------------

'    If ComboBoxKeyPress(KeyAscii) <> "" Then KeyAscii = 0

'--------------------------------------------

On Error GoTo Err

Dim i As Integer

With Screen.ActiveControl

    If .ControlType = acComboBox And (KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Then

    '只能由組合框輸入小寫字母時(shí)調(diào)用

        ComboBoxKeyPress = Nz(HZPY(Left(.Text, .SelStart))) & Chr(KeyAscii)

        While HZPY(Left(.ItemData(i), .SelStart + 1)) <> ComboBoxKeyPress And i < .ListCount - 1

            i = i + 1

        Wend

        If i <> .ListCount And Mid(.ItemData(i), .SelStart + 1, 1) <> Chr(KeyAscii) Then

            ComboBoxKeyPress = Left(.ItemData(i), .SelStart + 1)

            .Value = Null

            SendKeys ComboBoxKeyPress

        Else

            ComboBoxKeyPress = ""      '為英文時(shí)不代換,否則會(huì)進(jìn)入死循環(huán)

        End If

    End If

End With

Exit Function

Err:

    ComboBoxKeyPress = ""

End FunctionFunction HZPY(hzstr As String) As String

Dim p0 As String, C As String, str As String

Dim i As Integer, j As Integer

p0 = "吖八嚓咑妸發(fā)旮鉿譏譏咔垃呣拿謳趴七呥仨他哇哇哇夕丫匝咗"

For i = 1 To Len(hzstr)

    C = "z"

    str = Mid(hzstr, i, 1)

    If Asc(str) > 0 Then

        C = str

    Else

        For j = 1 To 26

            If Mid(p0, j, 1) > str Then

                C = Chr(95 + j)

                Exit For

            End If

        Next

    End If

    HZPY = HZPY + C

Next

End Function
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則

QQ|站長(zhǎng)郵箱|小黑屋|手機(jī)版|Office中國(guó)/Access中國(guó) ( 粵ICP備10043721號(hào)-1 )  

GMT+8, 2024-10-23 06:24 , Processed in 0.246780 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表