字幕表 動画を再生する
- [Adam Wilbert] Access shifts with many built-in functions
- Adam Wilbert] 多くの組み込み関数を持つアクセスシフト
that you can use to modify the data
データを修正するために使用できるもの
that's stored in your data tables.
を、データテーブルに保存しています。
Occasionally however, these won't be enough
しかし、これだけでは十分でない場合もあります。
to calculate exactly what you need.
を使えば、必要なものを正確に計算することができます。
Or perhaps you want to save a complex calculation
あるいは、複雑な計算を保存したい場合
to use over and over again.
を何度も繰り返し使用することができます。
In these cases, you'll want to create
このような場合は
your own custom functions in Visual Basic.
Visual Basicで独自のカスタム関数を作成することができます。
We can start that process us by coming up to the Create tab
このプロセスを開始するには、「作成」タブに進みます。
and coming over to the Macros and Code section
をクリックし、「マクロとコード」セクションに移動します。
on the far right and clicking on Module.
をクリックし、「モジュール」をクリックします。
That'll open up a brand new program
新しいプログラムの始まりだ
called Microsoft Visual Basic for Applications.
Microsoft Visual Basic for Applicationsと呼ばれるものです。
And this is where we get the acronym, VBA.
そして、ここからがVBAという頭文字の由来です。
I want to make sure that we have three different windows open
3種類のウィンドウを開くようにしたい
here on our screen.
をスクリーンでご覧ください。
We've got the Project Explorer, up here on the upper left,
左上にあるのがプロジェクトエクスプローラーです。
the Properties Window on the lower left
左下のプロパティウィンドウ
and the Immediate Window across the bottom.
とイミディエイトウィンドウを横切る。
If you're missing any of these windows,
いずれかのウィンドウが欠けている場合。
just come up here to View and you can toggle them on
を表示させることができます。
with the Immediate Window option, Project Explorer
イミディエイトウィンドウオプションで、プロジェクトエクスプローラー
or Properties Window.
または「プロパティ」ウィンドウを開きます。
In the Properties Window,
プロパティウィンドウで
I'm going to change the name of our module
モジュール名を変更します
from the default name to Myfunctions.
をデフォルトの名前からMyfunctionsに変更します。
You'll see that change is made up here
その変化は、ここで作り上げられることがわかるでしょう
in the Project Explorer and in the title bar of the window
をプロジェクトエクスプローラとウィンドウのタイトルバーに表示します。
that's currently open here.
は、現在こちらで公開されています。
In this window, is where we're going to type in
このウィンドウで、次のように入力します。
the different functions that we want to create.
を、作りたい機能の種類によって使い分けています。
I'll come down to the line below, Option Compare Database
下の行に降りてくる、Option Compare データベース
and we'll type in the keywords, Public Function.
と入力し、Public Functionというキーワードを入力します。
Public means that this function is going to be available
公開とは、この機能が利用できるようになることです
outside of the code module.
をコードモジュールの外側に置く。
So, we can use it in queries and forms, for example.
だから、例えばクエリーやフォームに使うことができるんだ。
The function that I want to create is going to calculate an age
私が作成したい関数は、年齢を計算するものです。
when we give it a date of birth
生年月日を入力すると
I'll name it simply, Age and open a parenthesis.
シンプルにAgeと名付け、括弧を開けてみる。
Inside of the parentheses,
括弧の内側。
we're going to type in the different arguments
を入力します。
or the data that we're going to pass in to the calculation.
または計算に渡すデータです。
So, in order to calculate an age, we need a date of birth.
つまり、年齢を計算するためには、生年月日が必要なのです。
I'll just call it DoB for short.
略してDoBと呼ぶことにします。
Then, Access needs to know what type of data to expect.
次に、Accessはどのような種類のデータを期待するかを知る必要があります。
Now, obviously, date of birth is going to be a date data type,
さて、当然のことながら、生年月日は日付データ型になります。
so I'll type in as Date.
ということで、Dateと入力することにします。
We'll finish the Public Function declaration
Public Functionの宣言を終わらせる
with a closing parenthesis and press enter
を閉じ、Enterキーを押します。
to come down to the next line.
をクリックすると、次の行に降りてきます。
When I do so, Visual Basic adds in
そうすると、Visual Basicが追加で
the End Function line down below.
は、下のEnd Functionの行です。
In between these two lines,
この2行の間に
is where we're going to type in the different steps,
は、さまざまなステップを入力するところです。
to calculate an age given a date of birth.
で、生年月日から年齢を計算することができます。
We'll start that process by typing in Age equals.
そのために、まずはエイジイコールと入力することにします。
Now, there's lots of different ways to calculate an age
さて、年齢を計算する方法はいろいろとあるのですが
given a date of birth.
生年月日を指定する。
One way is to take today's date
一つの方法は、今日の日付を取ることです
and subtract the date of birth
で、生年月日を引くと
and that'll give us the number of days that have elapsed
で、経過日数がわかります。
between the two.
の間にある。
In order to get today's date,
本日の日付を取得するために。
I'll use one of Access's built-in functions,
Accessの組み込み関数の一つを使ってみます。
that's simply called, Date.
というのは、簡単に言えば、「Date」です。
That'll give us today's date and we'll simply subtract DoB.
これで今日の日付がわかるので、あとは単純にDoBを引くだけです。
This is the date that we pass in, when we run this function.
これは、この関数を実行するときに渡す日付です。
Now, I want to make sure that this calculation happens first,
さて、この計算が最初に行われるようにしたい。
so, I'm going to wrap that in parentheses.
ということで、それを括弧でくくる。
Then we'll come to the end
そして、最後に来るのは
and I'll divide the whole thing by 365.25.
と、全体を365.25で割ってみる。
This is the average number of days in a year.
これは、1年間の平均日数です。
At this point, the function is going to return
この時点で、この関数は
a very precise fraction of a year
しゅくねんひゃくぶん
and I actually want to just drop off the remainder
で、実は残りを投下したいのです。
and return the whole number of years.
と入力し、全体の年数を返します。
So, we're going to wrap this whole thing here
そこで、ここで全体を包み込むように
in another function.
を別の関数で使用します。
I'll type a parenthesis at the very end,
一番最後に括弧を打ちます。
I'll come back to the beginning,
最初に戻ってきます。
I'll type in another parenthesis
もう1つの括弧を入力する
and this function is going to be called Int.
で、この関数をIntと呼ぶことにします。
This will essentially just return the whole number of years
これは基本的に、全体の年数を返すだけです
that have elapsed without any extra days.
を、余分な日数なく経過させる。
So, there's our entire calculation.
そこで、全体の計算をします。
Let's come down here to the immediate window
ここで、すぐそばの窓まで降りてきてみましょう
and we'll test it out.
で、テストしてみます。
We'll do that by typing in a question mark,
クエスチョンマークを入力することでそれを実現します。
the name of the function, Age,
関数の名前、Age。
I'll open a parenthesis and we'll give it a date.
括弧を開けると、日付が出るんです。
Remember in Access, we use the date delimiters
Accessでは、日付の区切り文字を使用することを忘れないでください。
of the pound symbols around our dates.
日付の周りにあるポンド記号の
So, I'll type it a pound and then 10 slash 28 slash 1955.
だから、1ポンドと打って、10スラッシュ28スラッシュ1955とする。
We'll finish it with another pound symbol
もう一つのポンド記号で仕上げます
and a closing parenthesis.
と閉じ括弧があります。
This is the birth date for Microsoft co-founder, Bill Gates.
マイクロソフトの共同創業者、ビル・ゲイツの生年月日です。
And when I press enter,
そして、エンターキーを押すと
we'll see that at the time of the recording,
は、収録時に確認することにします。
that he is 62 years old.
62歳であること。
So, we can see our age function is working,
つまり、年齢機能が働いていることがわかるのです。
let's go ahead and create another public function.
では、もうひとつパブリック関数を作ってみましょう。
I'll come back up here into my window,
私はこの窓の中に戻ってきます。
I'll press enter to come down to another empty line
エンターキーを押して、別の空の行に降りてきます
and we'll start the process again with Public Function.
で、Public Functionからやり直します。
This function is going to take in two arguments,
この関数は2つの引数を受け取ることになる。
the first name and the last name of a person
めいじつ
and it's going to return a formatted string
で、フォーマットされた文字列を返します。
where we have last name, comma first initial.
姓と名のイニシャルをコンマで区切っています。
I'll name this function, FormattedName, open a parenthesis
この関数をFormattedNameと名付け、括弧を開けると
and the first argument is firstName
で、第一引数はfirstName
and we'll be passing that in as a string character type,
で、それを文字列の文字型として渡すことになります。
I'll type in a comma
コンマで入力する
and I can type in the second argument, which is lastName
で、第2引数に lastName を入力します。
and that'll also be a string data type.
で、これも文字列データ型になります。
I'll type in a closing parenthesis
閉じ括弧を入力する
and this time we want to specify
を指定したいのですが、今回は
that we want this returned as a string.
を文字列として返すようにします。
So, I'll type in as string one more time.
そこで、もう一回、文字列として入力してみます。
We'll come down to the next line
次の行に降りてくる
and type in the calculation here,
をクリックし、ここに計算を入力します。
so, formatted name is equal to
ということで、フォーマットされた名前が等しくなります。
and it's simply going to return just the last name
で、単純にラストネームだけを返します。
and then we'll concatenate that
を連結して、それを
or join that to the text string,
またはそれをテキスト文字列に結合する。
so, I'll type in an ampersand, a double quote,
ということで、アンパサンド、ダブルクォートを入力します。
we're going to join that to a comma and then a space,
それをコンマとスペースでつなげます。
I'll type in another double quote and another ampersand.
またダブルクォートとアンパサンドを入力します。
We're going to join that string to a Left function,
その文字列をLeft関数に結合するんだ。
open a parenthesis.
は括弧を開けてください。
The Left function will process the first name
Left関数は、最初の名前を処理します
and I'll type in a comma and a one.
とコンマと1を入力します。
We'll finish that with a closing parenthesis,
最後に括弧を付けて終了します。
another ampersand, a double quote, a period
もう1つのアンパーサンド、ダブルクォート、ピリオド
and a double quote.
とダブルクォートを使用します。
Essentially what we're doing here, is building a string
基本的に、ここでやっていることは、文字列を構築することです。
that'll take the full last name,
であれば、フルネームでOKです。
join that to a comma and a space,
は、カンマとスペースにつなげます。
join that to the first initial of the first name
割る
and join that to a period.
を作成し、それをピリオドに結合します。
Let's come down to the immediate window again
もう一度、直下の窓に降りよう
and I'll click my mouse to insert the cursor
で、マウスをクリックしてカーソルを挿入すると
and we'll type in, FormattedName, open a parenthesis.
で、FormattedNameと入力し、括弧を開く。
This prompts me for the first name as a string
これは、ファーストネームを文字列で入力するよう求めるものです。
and remember, we need to type that in double quotes,
と、二重引用符で囲んで入力する必要があることを忘れないでください。
I'll type in "Bill", I'll type in a comma
ビル "と入力し、コンマを入力する
and then the last name as a string again
という文字列を追加し、さらに姓を文字列として追加します。
in double quotes, "Gates".
を二重引用符で囲んで、"Gates "と表記します。
I'll type in the closing double quote
締めのダブルクォートを入力する
and a closing parenthesis and press enter.
と閉じ括弧を入力し、Enterキーを押してください。
And I'm getting a compile error here,
そして、ここでコンパイルエラーが発生しています。
that's actually because I forgot the question mark
というのは、実はクエスチョンマークを忘れてしまったからです。
at the very beginning.
を、冒頭で紹介しました。
So, go ahead and type that question mark in, run it again
だから、その疑問符を入力して、もう一度実行してみてください。
and it returns it as expected, Gates comma B.
と入力すると、予想通りゲイツコンマBで返ってきます。
Let's go ahead and save our code module now,
それでは、コードモジュールを保存してみましょう。
I'll press the disc icon here on the toolbar
ツールバーのディスクアイコンを押してみます。
and it's going to prompt me to save the module name Myfunction,
と表示され、モジュール名Myfunctionを保存するように促されます。
then I'll just simply say, okay.
と言えば、あっさりOK。
So, that's a quick introduction
というわけで、ざっくりとした紹介になりますが
on how you can begin to create custom functions
カスタム関数を作成する方法について
in Visual Basic.
をVisual Basicで作成しました。
(upbeat music)
(アップビート・ミュージック)