How do I get the system time using VB.NET and copy it into the clipboard automatically?
Is there a built-in function in VB.NET for this?
I'm not sure what you mean by System Time, but if it's just the string representation of the current time then you can do the following.
ClipBoard.SetText(DateTime.Now.ToString())
This code will work in both Windows Forms and WPF.
Use DateTime.Now to get the time and Google for the code to copy to the clipboard.
Or take a look at this post.
Better use the DateTime.UtcNow method.
It returns time without local format and DST offset.
UTC will keep you out of trouble when storing data.
You can always format to local when export/display is needed.
Related
Long story short, I am dealing with an excel files, which need to be modified a little bit. As the files are coming on weekly basis, I decided to write a simple program via Access, which will help me to make the process fully automatic.
The first step was to upload the excel file into an Access database. I managed to achieve that by creating a custom function and inside to just use the "DoCMD.TransferSpreadsheet acImport" aproach.
The second step was to create two queries and update the table that I just uploaded. That was also pretty straight forward too.
However, the third step is what I am struggling with. Now when the table is updated I wanted to export it back to .xlsx format. However, when I do that no matter if I do it manually via the "External Data" tab or simply use "DoCMD.TransferText acExport" approach I noticed that a few columns that have a space after the end of the string are trimmed automatically. For example, original:"string ", but after exporting it is changed to "string".
I would be really grateful if someone can tell me how to specify to Access that the space after the string is intended and not done by mistake? Preferably with a VBA solution than having to do it manually. Thank you in advance for the help!
PS: I know that .CSV format would be way better, but sadly I need it to be in a XLSX format.
Thanks for adding DateTimeInput! I have a bit of a problem with it, though. It converts the entered time into UTC using my local timezone (decreases 2 hours from the entered time). Is there a way to make it simply save the time that's entered?
I didn't find any info about any options this component accepts.
EDIT:
Nobody has any answer to this problem? I'm sure I'm not the only one who wants to be able to store date/time values from the UI "as-is", instead of some automatic conversions.
In our case we enter certain measurement data from different locations. The location of the UI user here is irrelevant and the automatic conversion simply messes things up.
I am designing my first database from the ground up and I have learned a lot in the last few weeks. One thing that has been eating at me though is that on my login page I have a simple unbound text box with its control source as =Date().
This works perfectly well on the computer I use most days, but any other computer in the facility I am working out of displays #NAME? instead of the date.
I have tried changing to =Now() and it works fine on all computers. Apparently only =Date() has issues. If the fields control source is changed back to =Date() I am informed that The function you entered can't be used in this expression.
I have checked the MS-Access versions and tried on a machines with and without access.
I really need Date() to work because it is used elsewhere in more vital areas of my code and I may not be able to use Now() in its place. Any ideas as to why this may not work on any PC besides my own?
Date() is controlled by the Visual Basic For Applications X.XX reference. Make sure the reference is installed and any competing references are removed.
To do this, open the VBA window and go to Tools --> References and ensure the proper one is checked.
I suspect the issue is user's region and language settings for the date. I noticed this when I changed computers and my files wouldn't save anymore due to the slashes in the date. You should try the format() function so that your dates always have the same format and can be used across machines independent of the user's settings.
Sub Foo()
Debug.Print Date
Debug.Print format(Date, "yyyy-mm-dd")
End Sub
Output
7/23/2015
2015-07-23
I'm trying to amend our content management system so it'll handle SQL database failures more gracefully. It's a bunch of ASMX pages, and a Helpers.vb file in which I've written a SQL connection tester function.
Each of the ASMX pages call the same function.
I need to create a variable I can check that's persistent and performant, otherwise I'm going to have fall back on something disasterously slow like reading a text file every time I set up a sql connection string.
I've tried using application caching, but either it doesn't work in the context of my helpers.vb file, or I've made a mess of the syntax. One problem that's already stymied some of the approaches I've found via google - I can't use 'Import System.Web.Caching' - IntelliSense doesn't show the 'Caching' part.
Has anyone got any example code that might get me up and running? Or an alternative approach?
#Mike,
Many thanks, now I'm using HttpRuntime.Cache correctly... it works!
Thanks everyone for taking the time to post :)
I used p/invoke GetSystemTime() method in my application to get the current system date time but it is giving wrong values any solution for this..
Ah, time and the CF and WinCE. What fun! Along with all the other fine answers you've received there are other things to know:
The OS stores LocalTime, not UTC so GetSystemTime ends up getting LocalTime and them adjusting that backwards based on your timezone and DST, so if the local time is right but SystemTime is not, then you have a TZ or DST setting wrong.
DST may or may not be right due to congress changing it, so a QFE may be required by the OEM
DST may be on or off in the registry
The CF caches timezone bias as startup, so any adjustment of the timezone renders DateTime.Now incorrect until you restart your app
Not all devices can persist time across a power loss (or even a reset)
Time will "float" throughout the day. how badly (milliseconds to seconds) depends on the actual hardware implementation
Why don't you use DateTime.Now ?
What is the problem?
Is your p/invoke signature correct?
Is you struct laid out correctly?
How are you dealing with the struct pointer being 'returned' ?
If the time returned is off by one hour, then you're running into a Daylight Savings Time bug (which can be fixed with a hotfix).
GetSystemTime returns the Coordinated Universal Time (UTC). You may be looking for just the local time, in which case you want to call GetLocalTime instead (or just use DateTime.Now or DateTime.UtcNow, and skip the PInvoke stuff).
What do you mean by wrong values?
Since you are asking about Windows CE, it might be that your system does not save the RTC and does not sync on boot resulting in not having the correct time at all.
This is platform specific. Is the time and date correct in the taskbar (assuming you have that in the image)?