アクセス担当のまみです。
String関数って指定した文字の頭文字を指定した回数繰り返した値を返してくれます。
今回は、String関数についてお伝えしますね。
Contents
String関数
指定した文字列の頭文字を指定した数値の数だけ繰り返した値を返してくれます。
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/hatenakun2.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
使用するテーブル
「T_サンプルデータ」
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb776.jpg)
さっそくクエリを使ってString関数を使ってみましょう。
クエリでString関数を使用する
String関数を使って、製品名の頭文字を3回繰り返してみましょう。
![](https://pc.arthhuman.com/wp-content/uploads/2018/11/cmb561.jpg)
リボンの①「作成」タブから②「クエリデザイン」を選択し、「T_サンプルデータ」を追加します。
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb785.jpg)
③フィールドに直接入力してもいいですが、右クリックして「ズーム」を選択すると、大きな画面で編集できます。
④「String: String(3,[製品名])」と入力して「OK」を左クリックしましょう。
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
クエリの結果
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb786.jpg)
クエリは「Q_String」と名前をつけておきましょう。
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/hatenakun2.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/hatenakun2.jpg)
あっちょっといいこと思いついたかも!
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/hatenakun2.jpg)
前に幼稚園の先生と話してたんだけど、数字や言葉を楽しく覚えられるような簡単な遊びとかあったら教えて欲しいって言われてたんだ。
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/hatenakun2.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
[quads id=2]
フォームでString関数を使用する
ではフォームを作成してみましょう。
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb787.jpg)
ボタンを1つテキストボックスを3つ配置します。
ラベルの標題 | テキストボックスの名前 |
文字を入力 | tx文字 |
数字を入力 | tx数値 |
結果 | tx結果 |
ボタンの標題 | ボタンの名前 |
実行 | btn実行 |
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb788.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb790.jpg)
①「btn実行」を選択した状態でプロパティーシートの②「イベント」タブからクリック時に「イベントプロシージャ」を選択してコードの画面を表示します。
「Private Sub btn実行_Click()」と「End Sub」の間に下記のコードを追加しましょう。
Dim strChr As String Dim strNum As String strChr = Me.tx文字.Value strNum = Me.tx数値.Value Me.tx結果.Value = String(strNum, strChr)
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb791.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/hatenakun2.jpg)
みかんって入力して数値は2!
![](https://pc.arthhuman.com/wp-content/uploads/2018/12/cmb792.jpg)
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/kyara1-r.jpg)
漢字もひらがなの方がいいかな?
![](https://pc.arthhuman.com/wp-content/uploads/2018/10/hatenakun2.jpg)
フォームには「F_String」と名前をつけておきましょう。
コードをコピーされたい場合は下記の記事を参考にしてください。
まとめ
String関数ってデータ入力やデータ解析の際にどんな使われ方をするのかなって思ってしまいます。
私が知らないだけなのかもって思ってしまいますが、発想の転換でゲームとか作れるのかもしれませんね。
他の関数とかも組み合わせて案外面白いゲームが作れるのかも!