If you're referring to a Windows Phone, then there is no way to unlock the device without the 4-digit code without losing data. It seems that the only way you can use your phone is to perform a hard reset on your phone. There are two ways through which this can be done:
1. Through your Windows Phone account in a web browser - head over to "My Phone" and click on "Erase".
2. Through a button combination as found in this
Windows Phone FAQ page:
a) Press and hold the Volume down Volume down button and PowerPower button buttons at the same time until you feel a vibration (about 10-15 seconds).
b) When you feel the vibration, immediately press and hold the Volume down Volume down button button until you see a large exclamation mark.
c) Once the exclamation mark appears, press the following four buttons in this order: Volume up Volume up button, Volume down Volume down button, Power Power button, Volume down Volume down button. Your phone should now reset and restart itself. (It might take a while for the reset to finish.)
Note that in both of these processes you will be erasing all of the data from your Windows Phone.
If you're referring to a different device, then you may need to create a Windows Central account to respond to this thread and let us know what specific device you're referring to.