忍者ブログ

[PR]

2024年12月04日
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

漢字からフリガナを変換する

2009年06月24日

漢字の文字はあるけども、フリカナをイチイチ入力するのってめんどいですよね。
そんな時、エクセルオブジェクトを使いフリガナを抽出するサンプルを下記に書いておきます。

 

Dim CN As ADODB.Connection
Dim RS As ADODB.Recordset
Dim SQL As String
Dim TMP As String
Dim EXL_OBJ As Object

Set CN = CurrentProject.Connection
Set RS = New ADODB.Recordset
Set EXL_OBJ = CreateObject("Excel.Application")
SQL = "SELECT * FROM [テーブル名]"
RS.Open SQL, CN
Do Until RS.EOF
    
        TMP = EXL_OBJ.GetPhonetic(RS!漢字)
   
        SQL = "UPDATE [テーブル名] SET フリガナ='" & TMP & "' WHERE 主キー='" & RS!主キー & "'"
        CN.Execute SQL
    RSA.MoveNext
Loop

 

拍手[1回]

PR

Accessでテーブルを非表示にする

2009年06月14日

MS-Accessでテーブルを非表示にしたい場合のVBA。




Function テーブル非表示()

Dim TDF As DAO.TableDef

For Each TDF In CurrentDb.TableDefs
    TDF.Properties("Attributes") = dbHiddenObject
                                    'dbHiddenObject または 1  を入れる
Next

End Function

Function テーブル表示()

Dim TDF As DAO.TableDef

For Each TDF In CurrentDb.TableDefs
    TDF.Properties("Attributes") = 0
Next

End Function

Function TableDefプロパティ一覧()

Dim TDF As DAO.TableDef
Dim A As Long


For Each TDF In CurrentDb.TableDefs

A = 0
Debug.Print TDF.Properties.Count

    Do
        Debug.Print TDF.Properties(A).Name
        A = A + 1
    Loop Until TDF.Properties.Count <= A
   
Next

End Function

 

拍手[1回]

VB・VBAでハッシュ値を求める

2009年06月10日

VBやVBAでハッシュ値を求めたい時のサンプルです。

下記はVBAで作ってあります。



 

Option Explicit

Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" _
                            (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, _
                             ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" _
                            (ByVal hProv As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptCreateHash Lib "advapi32.dll" _
                            (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, _
                             ByRef phHash As Long) As Long
Private Declare Function CryptDestroyHash Lib "advapi32.dll" _
                            (ByVal hHash As Long) As Long
Private Declare Function CryptHashData Lib "advapi32.dll" _
                            (ByVal hHash As Long, pbData As Any, ByVal cbData As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGetHashParam Lib "advapi32.dll" _
                            (ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, ByRef pcbData As Long, _
                             ByVal dwFlags As Long) As Long

Private Const PROV_RSA_FULL   As Long = 1
Private Const PROV_RSA_AES    As Long = 24
Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000

Private Const HP_HASHVAL      As Long = 2
Private Const HP_HASHSIZE     As Long = 4

Private Const ALG_TYPE_ANY    As Long = 0
Private Const ALG_CLASS_HASH  As Long = 32768

Private Const ALG_SID_MD2     As Long = 1
Private Const ALG_SID_MD4     As Long = 2
Private Const ALG_SID_MD5     As Long = 3
Private Const ALG_SID_SHA     As Long = 4
Private Const ALG_SID_SHA_256 As Long = 12
Private Const ALG_SID_SHA_512 As Long = 14

Private Const CALG_MD2        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2)
Private Const CALG_MD4        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4)
Private Const CALG_MD5        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5)
Private Const CALG_SHA        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA)
Private Const CALG_SHA_256    As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_256)
Private Const CALG_SHA_512    As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_512)

' Create Hash
Private Function CreateHash(abytData() As Byte, ByVal lngAlgID As Long) As String
    Dim hProv As Long, hHash As Long
    Dim abytHash(0 To 63) As Byte
    Dim lngLength As Long
    Dim lngResult As Long
    Dim strHash As String
    Dim i As Long
    strHash = ""
    If CryptAcquireContext(hProv, vbNullString, vbNullString, _
                           IIf(lngAlgID >= CALG_SHA_256, PROV_RSA_AES, PROV_RSA_FULL), _
                           CRYPT_VERIFYCONTEXT) <> 0& Then
        If CryptCreateHash(hProv, lngAlgID, 0&, 0&, hHash) <> 0& Then
            lngLength = UBound(abytData()) - LBound(abytData()) + 1
            If lngLength > 0 Then lngResult = CryptHashData(hHash, abytData(LBound(abytData())), lngLength, 0&) _
                             Else lngResult = CryptHashData(hHash, ByVal 0&, 0&, 0&)
            If lngResult <> 0& Then
                lngLength = UBound(abytHash()) - LBound(abytHash()) + 1
                If CryptGetHashParam(hHash, HP_HASHVAL, abytHash(LBound(abytHash())), lngLength, 0&) <> 0& Then
                    For i = 0 To lngLength - 1
                        strHash = strHash & Right$("0" & Hex$(abytHash(LBound(abytHash()) + i)), 2)
                    Next
                End If
            End If
            CryptDestroyHash hHash
        End If
        CryptReleaseContext hProv, 0&
    End If
    CreateHash = LCase$(strHash)
End Function

' Create Hash From String(Shift_JIS)
Private Function CreateHashString(ByVal strData As String, ByVal lngAlgID As Long) As String
    CreateHashString = CreateHash(StrConv(strData, vbFromUnicode), lngAlgID)
End Function

' Create Hash From File
Private Function CreateHashFile(ByVal strFileName As String, ByVal lngAlgID As Long) As String
    Dim abytData() As Byte
    Dim intFile As Integer
    Dim lngError As Long
    On Error Resume Next
        If Len(Dir(strFileName)) > 0 Then
            intFile = FreeFile
            Open strFileName For Binary Access Read Shared As #intFile
            abytData() = InputB(LOF(intFile), #intFile)
            Close #intFile
        End If
        lngError = Err.Number
    On Error GoTo 0
    If lngError = 0 Then CreateHashFile = CreateHash(abytData(), lngAlgID) _
                    Else CreateHashFile = ""
End Function

' MD5
Public Function CreateMD5Hash(abytData() As Byte) As String
    CreateMD5Hash = CreateHash(abytData(), CALG_MD5)
End Function
Public Function CreateMD5HashString(ByVal strData As String) As String
    CreateMD5HashString = CreateHashString(strData, CALG_MD5)
End Function
Public Function CreateMD5HashFile(ByVal strFileName As String) As String
    CreateMD5HashFile = CreateHashFile(strFileName, CALG_MD5)
End Function

' SHA-1
Public Function CreateSHA1Hash(abytData() As Byte) As String
    CreateSHA1Hash = CreateHash(abytData(), CALG_SHA)
End Function
Public Function CreateSHA1HashString(ByVal strData As String) As String
    CreateSHA1HashString = CreateHashString(strData, CALG_SHA)
End Function
Public Function CreateSHA1HashFile(ByVal strFileName As String) As String
    CreateSHA1HashFile = CreateHashFile(strFileName, CALG_SHA)
End Function

' SHA-256
Public Function CreateSHA256Hash(abytData() As Byte) As String
    CreateSHA256Hash = CreateHash(abytData(), CALG_SHA_256)
End Function
Public Function CreateSHA256HashString(ByVal strData As String) As String
    CreateSHA256HashString = CreateHashString(strData, CALG_SHA_256)
End Function
Public Function CreateSHA256HashFile(ByVal strFileName As String) As String
    CreateSHA256HashFile = CreateHashFile(strFileName, CALG_SHA_256)
End Function

' SHA-512
Public Function CreateSHA512Hash(abytData() As Byte) As String
    CreateSHA512Hash = CreateHash(abytData(), CALG_SHA_512)
End Function
Public Function CreateSHA512HashString(ByVal strData As String) As String
    CreateSHA512HashString = CreateHashString(strData, CALG_SHA_512)
End Function
Public Function CreateSHA512HashFile(ByVal strFileName As String) As String
    CreateSHA512HashFile = CreateHashFile(strFileName, CALG_SHA_512)
End Function
 

拍手[11回]

VBで正規表現

2009年06月08日
VBやVBAで正規表現を行うのは、下記の通り。
 
Function 正規表現CK(STRSRC As String, LNGPTN As Long) As Boolean
'LNGPTNの設定値
'1=英字大文字
'2=英字子文字
'3=数値
'4=ひらがな
'5=全角カタカナ
'6=半角カタカナ
'7=特殊文字
'8=SQLServerの引数で良くない文字(SQL インジェクション簡易対策)
'パターンの文字があった場合にTrueを返す
On Error GoTo ERR_SKP:
   
Dim OBJREG As Object
Dim STRPTN As String
Dim RET As Boolean
Set OBJREG = CreateObject("VBScript.RegExp")
OBJREG.IGNORECASE = False
Select Case LNGPTN
    Case 1
        STRPTN = "[A-Z]"
    Case 2
        STRPTN = "[a-z]"
    Case 3
        STRPTN = "[0-9]"
    Case 4
        STRPTN = "[ぁ-ん]"
    Case 5
        STRPTN = "[ァ-ン]"
    Case 6
        STRPTN = "[ァ-ン]"
    Case 7
        'アスキーコードの32~47、58~64、91~96、123~126
        STRPTN = "( |!|""|#|\$|%|&|'|\(|\)|\*|\+|,|-|\.|/|:|;|<|=|>|\?|@|\[|\\|\]|\^|_|`|\{|\}|~|\|)"
    Case 8
        OBJREG.IGNORECASE = True
        STRPTN = "(;|'|--|/\*|\*/|xp_|SELECT|UPDATE|INSERT|DELETE|TRUNCATE|ALTER|EXECUTE|EXEC|DROP|CREATE)"
End Select
OBJREG.Pattern = STRPTN
RET = OBJREG.Test(STRSRC)
If RET = True Then
    ' マッチした場合の処理
Else
    ' マッチしなかった場合の処理
End If
' 戻り値をセットします。
正規表現CK = RET
   
Set OBJREG = Nothing
Exit Function
   
ERR_SKP:
If OBJREG Is Nothing Then
Else
    Set OBJREG = Nothing
End If
End Function
 

拍手[0回]

Environ関数

2009年06月08日
APIでもPC名等だせますが、VBで簡単に出す方法
 
Private Sub Environ一覧()
    Dim K As Integer
    Dim S As String
    Do
        K = K + 1
        S = Environ(K)
        Debug.Print S
    Loop Until S = ""
End Sub
Kの部分は文字列で指定しても良い

拍手[0回]