Tuesday, July 30, 2024

Weather API in Microsoft Access, Part 2

 How to Use a Web-Based Weather API To Pull Current Weather Data into Microsoft Access, Part 2


This is part two of my weather API series where we're getting the weather from the internet with an API, and it's pretty cool stuff. If you haven't watched part one yet, go watch that so you know what we're talking about, and then come on back.

Alright, so yesterday we learned all about the API call we have to do, the URL that we have to use, this guy, right, to get what we want. There are other forms of this too. I put some other ones online for you. Here are two other versions. You can either say Q equals and then your location like that, or instead of Q you can say zip and use a zip code. I don't know what they have available for other countries, but here in the US you can use a zip code. You can probably do this in the UK and Canada too. You'll have to look if you're somewhere else, sorry.

And then you can also use latitude and longitude, which was pretty funny because the first time I did this, I just put in 81.9 for my longitude, and I forgot the negative. So it had me somewhere way out on the other side of the world. But these are the three different methods you can use. And there are tons of different options too. You can change the units from Imperial, which we talked about last time. There are different language settings. So read their website for a lot more information. I'm just going into the basics here.

But I'm gonna stick with the zip code version, I think, in this one because that's just the easiest; punch in a zip code. Okay. We're gonna do some modifications on this guy. So let's design this. The first thing is let's change this today's date to current, oh, I'm sorry, zip code. We're gonna put the zip code in first. So this guy, we no longer need a control source. Leave that unbound. Let's change the name of this to just zip. It's no longer a short date. And I am going to put a default value in there of 33909. That's my zip code in Cape Coral, Florida.

Now for this class, we don't need these buttons. You guys can go away. Let's change this. While I'm at it, real quick, why not? Weather API, whoops, weather API. And I'm gonna slide you over here. I'm gonna put this button down below it. All right, get current weather. And we're gonna status the response that comes back from the API. So we'll keep this little status box here. And if you don't know what the status box is, I use this in a lot of my videos. I got a whole separate video on the status box. Here it is, I'll put a link down below. It's basically just a message box alternative. Instead of message boxing stuff and popping something up, you can just put it in this little gray box.

All right, now get current weather. We are going to go to build event, see what we got in here. All right, so this guy basically calls a function called getUTCTime and then just displays the status there. So we're going to change this getUTCTime. And I believe that is in this getTime module. So if we open that up, here is this stuff. All right, let's change some stuff around here. So we're going to say instead of getUTCTime, we're going to change this to getWeather as a string. We're going to return a string back, all the information that comes back to us. We need the HTTP object, the response text, the URL. We don't need this, this lns. Okay?

Now, my URL is where you're going to put your URL that we've been building. I'm going to copy it from where I have it, my clipboard. Where are you? Where's the clipboard? Oh, wrong one. There you are. I found you. There you are. Okay, let's copy this. Copy, move you out of the way, and paste right here URL. Boom, there it is. It's pretty long. Let's split it up. I'm going to split it up right here after that question mark. And we'll put it on the next line like that. Now the zip code, we're going to specify on the form. Now this is a module, so we have to use the full name of that form, okay, which is what we're going to close off our quotes and forms main menu F zip is the name of the field. I just smacked that right inside the string there, okay?

The next thing we need is the API key. Now I'm going to put the API key in a separate module because if I put it up here, I'm going to accidentally flash it at least three times during the video, and I don't want you getting my API key. So I'm going to come over here and I'm going to create a new module. And I'm going to say global constant API key equals, and I'm going to put my API key right there. And then I can use this constant called API key, and I don't have to worry about you seeing this. In fact, I'm going to save this as Rick's API key. And you can come in here if you want to put your API key in here or just move this into there. Whatever you want to do. I don't care. Alright. But I'm gonna paste my API key in there and then you're not going to see this anymore. Okay. So the API key is in there. Now I can take this and just delete that and put the API key out here. See? And it just goes out and gets that constant. Nice and easy.

Okay, so we got there, we got this is the same, this is the same, this is the same, this is the same, this is the same, and right down here we get s equals response text. All I'm gonna do is I'm gonna get rid of all of this stuff here. That's where we do the calculations for the date and stuff. And I'm just gonna say get weather equals response text. So this guy is literally just going to go out to this address, get the data back, and return it as part of this function call. Pretty easy.

All right, let's go back to the main menu. All right, main menu, right-click, build event, here's the button for that. Now it's no longer getUTCTime and it's no longer a date. This is going to be s as a string and s is going to be get weather. And what are we going to do with it? Well, we're just going to status it. Status S. All right. Save it. Give me a debug compile, make sure everything is good, everything compiles. Okay, great. And let's come over here, close it, open it. That moves. I got code in here that moves. This is one of my older ones. I used to have in this Rixmod keep out, I used to have code in here that would move the startup window. I'm just going to exit sub this. You can keep this if you want to. This is so that when I log on, it would put the window in the middle of my screen for recording purposes, but I don't use it anymore.

Okay, let's try that again. Get weather and boom, there it is. It went out and grabbed this stuff from the web, from the website we specified, openweathermap.org. All right. Now, while we're building this, I'm going to take all of this code that's in here. It's on a shift F2. We're going to... Oh, that's really big. Let's move this like this. I want to copy all of this and drop this on my clipboard as well because there's a lot of data in here. We need to go through and parse it.

So just copy that to your clipboard here, come back to my handy-dandy clipboard. We're gonna put this on the bottom. I use a clipboard constantly when I'm programming, constantly, just to put stuff back and forth. All right, so save this. Now, what we're gonna do next is parse the bits of data out of here that we need. For example, the temperature is right here. Let me just squeeze this a little bit so you can see it better, there we go, okay. Temperature value equals right there. You can probably even grab it from here. Okay. That's what you want. That's the start of the temperature value, and it's going to end with the next quote. So we're going to use my find between function to pull that out of this string.

Once again, as a reminder, here is the find between video where I explain how this code was built. If you are a gold member, you can come to the find between page, there's a link down below and you can go right to the code vault and grab the find between code. If you're a silver member or you're not a member at all, here it is, get typing. I'm just going to hit copy and now it's on my clipboard. See, benefits of being a gold member or platinum if you want.

All right, back over here, let's go to the global module and I'm just going to paste that in the bottom here under my sleep function. Boom, there it is. So now I've got findBetween. And again, if you want to learn how this thing was built, go watch the findBetween video. Okay? Okay.

Now, back to here. Let's make another variable real quick, t as a string. And we're going to say t equals findBetween. Where's the string? Well, that's in the status box. That's the box on the main menu that's got the data in it, right, status box. All right, what's the start of the string? Now, put a set of quotes like that to start with, trust me. Okay, now go back to your clipboard. That's the text you're looking for for the beginning of it. Notice it's got a double quote inside there. Now what you have to do in Access, this goes back to beginner or early expert level stuff. For every one of these inside of a string, you have to put a double double quote. All right? So I'm going to take this, copy it, come back over here inside those quotes, and paste that, but I got to make one more quote because this becomes a double double quote inside the quotes. That says there's a literal quote there. This is a topic that's confusing for a lot of people including advanced developers. I personally sometimes have to stop and think a couple of times about how many quotes do I need. So go watch this video if you need more help. I talk a lot more about the double double quotes problem. Okay, go watch this.

All right, so back to this guy. All right, copy. Now we need, what are you looking for for the end of the string? Well, that's just the closing quotes. So it's going to be quote, quote, quote, quote. That's a literal set of quotes inside of a string. Now we're just going to message box T, and we'll see what that value is. Save it. Do a debug compile. Everything checks out. Let's come back over. You get out of the way. Come back over here, hit the button, bink, and there's my temperature, 79.05. And that's about right, it's about 79 degrees outside, not too bad tonight. If you look in here you should be able to see that, 79, there it is, 79.05. I don't think their feels right is right because it's a little more humid, so this might be something with their data. I've been working with this for the past couple of hours and that hasn't changed. The current temperature is always the same as the feels right. What's the humidity? 92, yeah, it's up there. It feels like it's about to rain, folks.

All right, now what you can do is, you can take this, instead of putting it in T and message boxing it, why not throw it in some more fields in here. Design view. Copy this guy, copy, paste. I'm going to put that over here. We're going to do current temp. We're going to do, let's change this here to current temp. Oh, come on. Current temp. Can you guys read this text okay? Is this big enough for you to see? I know YouTube sometimes compresses the video and makes it a little harder to see this. I know my website has clearer video than YouTube does. So, let me know. Post in the comments down below. If you have problems reading this text, let me know. We'll do current temp, we'll do feels like, even though I don't think it's right. Feels like, I know I zoom in on text if it's something that's really important to see. Why is my box so big? There it is, feels like. Hit okay. That's interesting to know by the way. If you zoom in like this and then let's say you change the dimensions of this box. If you hit OK, it saves the dimensions. If you hit Cancel, it doesn't. If you open it up again, it's back to its old dimensions. I discovered that a little while ago. Feels like, I got the location. And then we'll do the current date and time. Date and time.

It's funny because date is a reserved word in Access. Time is a reserved word in Access, but date time is not. So you can use date time safely. But don't always use like date or just time. Always have it like, you know, order date, appointment date, that kind of stuff. Never use just date. Just like, you don't want to use name. Those are all reserved words. All right. Date time, location, feels like, and current temp. Come over here and we'll do the same thing. Now, we can get rid of the T. We don't need T anymore. I just put that in there so we can message box. All right. We're going to say, right here, it's Current temp equals that. I'm just going to copy and paste this. Copy, paste, paste. We only need three of them because date time is just a date time equals now, like that. All right, we have feels like and we got location. 

All right, now let's take a look at our text. Feels like is, where are you? That's up here. Let me see. Yeah, I like to squish this so you can see it all together. All right, there it is. Feels like value equals right there. All right, that's what we need. Basically, it's just replacing temperature with feels like in the other one. All right, just like that. Location, what does location look like? Location is here. Okay, so we need... Now, I've noticed, I did some playing with this before, and I've noticed that city ID is always zero unless you specify a city ID. You can give a city ID. If you guys find out that this is different, let me know. And also, when you do this, no matter how you get it, whether it's the zip code or not, it'll give you the longitude and latitude. That's pretty cool. Okay? 

So, for this one, we're looking for city ID equals zero and the name equals. And notice all the quotes in there, so we got to fix all of those. All right, so for location, it's going to be that, but then we got to go quote, quote, quote like that. And I think that's all we have to do. Yeah. Okay. All right. Save it. Debug compile. Click the button. Close it. Close it. Open it. And oh, wait a minute, I got my default value and all that. Well, let's go to that. Select all these guys, data. Default now. You forget to take that out of there. All right. Now save it. Close it. Open it. And click the button. And there we go. Obviously you gotta make this bigger. And yeah, see I don't think that I don't think there feels like feels right is working. Oh well, we can take that with them. There we go, beautiful. 

That's it, you can pull out whatever other information you want out of here. There's more stuff, there's wind speed and pressure and all that stuff. And they've got different other APIs. You can get forecasts, you can get little pictures, look at the little sunshine. They've got free and paid accounts, so go check them out. It's a good service and I endorse them fully and it works so give them your patronage if you can but I think that about covers it. If you like this stuff, if you like learning with me, and you want to learn more about VBA, come to my website. I got lots and lots of lessons. If you like my quirky teaching style and you enjoy my fantastic sense of humor, then come and learn some more from me.I got lots of lessons here. Also, consider signing up and joining if you want to become a member. Obviously, as a Silver member, you get all of my extended cut videos. You get a free beginner class every month, which you may or may not need.

Oops, someone just beamed in. Gold members can get an expert-level class every month, and Platinum members can get a free developer-level class every month after you've finished all the beginner and expert ones. There are all kinds of cool stuff available on my website. Come and check it out.

But that is gonna be your TechHelp video for today. I hope you learned something. Live long and prosper, my friends. I'll see you next time.

A special thank you and shout out to our Diamond Sponsor Juan Soto with Access Experts software solutions. They're manufacturing experts specializing in Microsoft Access and SQL Server. Juan is a 13-time Microsoft Access MVP. Check them out at accessexperts.com.

For a complete video tutorial on this topic, please visit https://599cd.com/WeatherAPI2

Monday, July 29, 2024

Weather API in Microsoft Access

 How to Use a Web-Based Weather API To Pull Current Weather Data into Microsoft Access


In today's video, we're going to talk about how to use a web-based weather API to pull current weather data into your Microsoft Access database. This is going to be both an expert and a developer video. I'm going to show you a developer method using an API call that is a lot more powerful, and you can get data results like this very easily.

What is an API? It's an application programming interface. That's simply a fancy way of saying you can ask somebody else to do something for you. Like a WinAPI, you can ask Windows to do something or give you some information. A web-based API is a web server that you can say, hey, give me some information, like what's the current temperature. So that's really all an API does, and there's tons of them, there's millions of them.

All right, today's question comes from Jeffrey in Linwood, Washington, one of my Silver members and an Access Expert student. Jeffrey says, I'm taking your Access Expert 24 class and in there you give a link to an RSS weather feed from Weather Underground. That feed no longer exists. Are there any other sites that you can recommend that still work?

Yes, Jeffrey, unfortunately, that class is about seven years old, and as always when dealing with websites, they come and go, they change things. So unfortunately, Weather Underground's API doesn't work anymore, but a quick little Google search shows AccuWeather has an API, and you just send it your zip code and it will tell you the weather conditions. You can do pretty much exactly what I show in that video.

Let me walk you through it real quick. Take any database, this is my TechHelp free template, you can grab a copy of this off my website if you want to. Copy that URL that I gave you to your clipboard. This guy. Yeah, it's a PowerPoint slide. Let me copy that. Copy, control C. Now in Access, we're going to go to External Data, New Data, From File, and then pick XML file. I know it's a URL, it's not a file, but just pick XML file. Now you're going to paste that URL in here. Change your zip code obviously to whatever you want. If you're out of the US, I'm not exactly sure what you do, there's instructions on their website. Hit OK. Now it's going to go out, it's going to find this table with some other stuff in it. You want to make sure you import the structure and the data. Hit OK. All right. Save the import steps if you want. I've got whole separate videos on saving import steps and doing imports in general. 

All right. Now you're going to get some tables imported over here. There's channel, there's image, what you're looking for is item right here. And there's your data. All right. There are some links in here to give you direct links to that stuff. There's a description right here. All right so you can pull this stuff out of this table just like you would any normal data. So if you want to learn more about this I cover it in a lot more detail in my Access Expert level 24 class. It's all the same from this point it's just the URL is different from the one that I use in this class. I also cover other things like setting up an SQL server database online. There's all kinds of cool stuff covered in this. I'll put a link to it down below.

Now this is the lesson for those of you who aren't programmers and don't want to learn VBA, but there's a more powerful method if you use a little bit of VBA. Now if you've never done any VBA programming before and you want to learn, this video will get you started. It's about 20 minutes long. It teaches you everything you need to know. After that, go watch my Web API video. This teaches you how the code works that can have Access, go out to the internet, connect to an API server, and bring back some information.

In this particular video, I show you how to get the current date and time from an internet time server. And it'll just bring back the current date and time, and that will prevent your users from, like, changing the system clock on their computer to act like they logged in to work an hour earlier. Okay? Okay. So go watch this. And also go watch my Find Between video. I build something called the Find Between function. Basically, when these internet servers give you data, they're going to give you a whole bunch of stuff. All kinds of stuff formatted really weird, and we can use my Find Between function to very easily pull out a bit of information. Like right here, we can very easily pull out the timestamp or the date using the Find Between. So go watch these. These are all free videos. They're on my website. They're on my YouTube channel. Go watch those and then come on back.

Okay, now on my website I am going to use the database that we built in the Web API video because it's got all the code that we need to connect to the web API service. We just have to change the URL. Instead of getting the current date and time, we're going to grab the weather from the Open Weather Map website. So, I'm going to come down here on the member section. I happen to be a gold member on my own website, so I can download this. All right, so here's that database. It's pretty simple. You hit Get Current Time and it gets the current time. It goes out to the web server. Now this one is pretty straightforward. Here's a get time mod that we built in the other video. OK, get UTC time. And it just literally goes to this web page and pulls in the information. Here's all the code that you need. OK, now the weather API from OpenWeatherMap is a little more involved, and you have to get something called an API key. Basically, you have to register. It's free, don't panic. But you have to sign up with your name and email address to request information. And they do that to prevent abuse. I think they give you like a thousand free calls a day, which is more than generous. There are, what, 1440 minutes in a day? So you could make a call every two minutes and be fine. Let's go sign up for that first. Let me show you how to do that. Their website is at openweathermap.org. Here's their signup page and I'll put links to all this stuff down below. Put in a username, email, password, repeat the password, all this stuff. You got to be 16 years or older, agree to their terms, hit the little thing here and then go create account. Once you verify your email address and all that good stuff you're going to log in, sign in successfully, then come over here and go to My API Keys.

When you create an account, you'll be given a default API key. You can also create your own. I created one called Weather. You just type in a name over here like I typed in Weather, hit Create or Generate, and it will give you a new key. It's not a super long code here. And yes, I blacked mine out. You cannot have mine. Make your own. They're free. Once you get an API key, copy it to your clipboard. We'll use it in just a few minutes. Now I will say that it took about 10 to 15 minutes between the time that I signed up and got my API key until it worked on their server. So it's not immediate, but it doesn't take long. So just wait a few minutes and it'll start working. All right. So paste your API key into Notepad and here's an example of the URL that you have to use to get data. I'm going to paste that in there as well.

It looks like this. I'm going to make it a little bit bigger. Here's what the URL looks like that you have to go to. And you can just put this in your browser. I'll put a copy of this down below so you can just copy and paste it. Here's the URL right out to about there. The question mark says here's where the parameters start. So I've got Q equals Cape Coral, Florida, USA, that's your location. It's pretty good about a wide variety of location formats. Then I specified at units equals imperial because the default for temperature is Kelvin. How very science-y of them. You can also switch it to Celsius. I'm in America, so we're still using the Fahrenheit, so that's imperial. And then app ID equals, this is where your application, your API key goes you put that right there don't use mine. Okay now you could take this whole thing and copy this and paste it right into your web browser and you'll get something that looks like this. Now this is a JSON response. JSON is a particular data format that's increasingly becoming the standard on the web. JSON can be a little difficult to parse information out of. I personally prefer XML if it's available, and with this particular API it is available. So I'm going to come back in here, and I'm going to add at mode equals XML, if you want. That's up to you. So again, now I'm going to copy this whole thing, drop it in my web browser. And now you get this guy. This is an XML formatted version of the same stuff.

This is just easier to pull data out of because now if you look at this you can see here's the temperature value equals so really what we're looking for is this. I want to find everything between everything between temperature value equals quote and then the next quote and we can use my find between function to very easily do this. We're going to get this information into Access and then just use find between we can also pull out the feels like values right there the humidity the pressure the wind speed all this stuff right what's the location name just to make sure you got the right spot right all right so now we know where to get the data from we've got our account set up we got our API key we can get the data in the web browser.

In tomorrow's video now, we're going to put it all together and use the API code that we built in the web API video to get that information, put it inside of our database, parse all the information out, and your end result will look something like this. We'll do that in tomorrow's class. So tune in tomorrow. Same bad time, same bad channel. You know the drill. If you remember, you can watch it right now because I'm gonna keep recording right now. But that's going to be your TechHelp video for today. I hope you learned something. Live long and prosper my friends. I'll see you tomorrow for part 2.

A special thank you and shout out to our diamond sponsor Juan Soto with Access Experts Software Solutions. They're manufacturing experts specializing in Microsoft Access and SQL Server. Juan is a 13-time Microsoft Access MVP. Check him out at accessexperts.com.

For a complete video tutorial on this topic, please visit https://599cd.com/WeatherAPI

Saturday, May 18, 2024

Relational Combo Boxes in Microsoft Access

Creating a combo box in Microsoft Access that retrieves its values from another table or query allows you to select a customer from a list when filling out service forms or orders.

To begin, it's essential to understand the difference between a value list combo box and a relational combo box. A value list combo box contains a small list of options directly within the form, such as titles like Mr., Mrs., Miss, etc. However, the downside is that you must update it on every form where it exists. A relational combo box, on the other hand, retrieves its list of options from a table or query, ensuring that updates are reflected across all forms automatically.

First, create the Titles Table. Open table design mode and create a new table. Add a field for TitleID (auto number) and TitleName (short text). Save the table as TitleT. Enter some sample data like Mr., Miss, Mrs., and Doctor.

Next, modify the Customer Table. Open the CustomerT table in design view. Add a new field TitleID of type number (long integer) to store the title. Save and close the table.

Then, add the Combo Box to the Customer Form. Open the CustomerF form in design view. Insert a combo box and select the option to get values from another table or query. Choose TitleT as the source. Include TitleID and TitleName fields in the combo box. Set the combo box to store TitleID in the customer table.

After that, design the Service Table and Form. Create a new table ServiceT with fields: ServiceID (auto number), ServiceDate (date/time), CustomerID (number), Description (short text), and Notes (long text). Save and close the table. Create a new form ServiceF based on ServiceT. Add a combo box to select a customer, using a query that concatenates the customer's first and last name for display.

When concatenating names, create a query that combines first and last names to display in the combo box. Use the query to source your combo box, ensuring both names appear together in the list.

Updating the combo box across forms is a significant advantage of using a relational combo box. If you add a new title in the TitleT table, it will reflect in all forms using the combo box.

Handling non-listed items can be done using the OnNotInList event to allow users to add new items directly from the combo box. This involves a bit of programming but is straightforward to implement.

Creating a relational combo box in Access not only simplifies data entry but also ensures consistency across your database. By following the steps outlined above, you can efficiently manage and update your lists without the need for repetitive updates across multiple forms. This method adheres to the principles of relational database design, ensuring data integrity and ease of use.

Here's a video tutorial that explains how to create a "relational" combo box that gets its list of values from a table or query: https://599cd.com/Relational.


Sunday, March 10, 2024

Abolish DST: Why Daylight Saving Time Must Go!

 


Abolish DST: Why Daylight Saving Time Must Go! (March 2024 Repost!) Find out more at: https://599cd.com/DSTisDumb   #dst #daylightsavings #daylightsavingstime #daylightsaving #daylightsavingtime 


Sunday, April 3, 2022

Wednesday, March 9, 2022

Microsoft Access Task Management Database

Microsoft Access Task Management Database. Design and Build Your Own! To Do List. Link to Customers. Custom Sort, Filter, More! #msaccess https://599cd.com/Tasks



Monday, January 31, 2022

How to Calculate Line Item Totals and Form Footer Totals in Microsoft Access

How to Calculate Line Item Totals and Form Footer Totals in Microsoft Access (Sum Function). Extended Cut: Display Sum on Parent Form, Store in Order Table #msaccess https://599cd.com/FormFooterTotals